Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: {fd}< and compound commands



2011-09-14 20:06:01 +0100, Peter Stephenson:
> On Wed, 14 Sep 2011 18:21:48 +0100
> Stephane Chazelas <stephane_chazelas@xxxxxxxx> wrote:
> > $ zsh -c '{echo foo >&$fd;}  {fd}> a'
> > zsh:1: parse error near `{'
> > (same for while/do/done constructs at least)
> > 
> > Is that the expected behavior? I can't see it being documented.
> 
> It is documented now.  This is all complicated stuff with wide
> implications for the parser; there's is no chance I will have to time to
> change it myself.
> 
> > That would be useful in
> > 
> > while read <&$fd var; do
> >   ...
> > done {fd}< file
> > 
> > to avoid having to worry about overriding fds 3-9.
> 
> There's no code automatically to close file descriptors anyway.
> Generally, that syntax isn't doing quite what the use of redirection
> syntax would suggest; it's permanently opening a file descriptor, not
> performing a possibly temporary redirection.
[...]

I just noticed ksh93 doesn't close the fd either, though it
supports compound commands:

$ ksh93 -c '{ echo test >&$fd; } {fd}> a; echo foo >&$fd; nl a'
     1  test
     2  foo

I suppose those were primarily meant to be used with "exec".

Looking at the changelogs for ksh93 and zsh, it seems both
shells implemented it within one week of each other (in April
2005), you guys surely must have mentionned it to each other, or
does that feature come from another shell/language?

KSH> 05-04-08 +Redirection operators can be directly preceded with {varname}
KSH>           with no intervening space, where varname is a variable name which
KSH>           allows the shell to select a file descriptor > 10 and store it
KSH>           into varname.

ZSH> 2005-04-12  Peter Stephenson  <pws@xxxxxxx>
[...]
ZSH>         * 21133: Doc/Zsh/redirect.yo, Src/exec.c, Src/parse.c, Src/text.c,
ZSH>         Src/zsh.h, Test/A04redirect.ztst: New {myfd}> syntax for
ZSH>         allocating file descriptors.

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author