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

Re: f() { ...; } > file



On 2008-11-05 at 21:20 +0000, Stephane Chazelas wrote:
> $ bash -c 'foo() { echo a >&3; } 3>&1; foo'
> a
> $ ksh -c 'foo() { echo a >&3; } 3>&1; foo'
> a
> $ zsh -c 'foo() { echo a >&3; } 3>&1; foo'
> foo: 3: bad file descriptor
> $ ARGV0=sh zsh -c 'foo() { command echo a >&3; } 3>&1; foo'
> foo: 3: bad file descriptor
> 
> It looks like zsh evaluates the redirection at the time the
> function is defined rather than when it is called.

Back to front.

I think that you're misunderstanding what bash et al are doing.  They're
not deferring the redirection, they're resolving the redirection at
definition time so that it remains bound to stdout.

% bash -c 'foo() { echo a >&3; } 3>&1; exec >/dev/null; foo'
% bash -c 'foo() { echo a >&3; } 3>&1; foo'                 
a
% ksh -c 'foo() { echo a >&3; } 3>&1; foo'
a
% ksh -c 'foo() { echo a >&3; } 3>&1; exec >/dev/null; foo'
% ksh -c 'echo $KSH_VERSION'
@(#)PD KSH v5.2.14.2 99/07/13.2
% ksh93 -c 'foo() { echo a >&3; } 3>&1; exec >/dev/null; foo'
% ksh93 -c 'foo() { echo a >&3; } 3>&1; foo'                 
a
% ksh93 -c 'echo $KSH_VERSION'              
Version M 93t 2008-07-25

Whereas zsh defers evaluating the file-descriptor:
% zsh -c 'foo() { echo a >&3; } 3>&1; exec >/dev/null; foo 3>&2'
a
%

(There might be a mistake in my analysis; collapsing into head-cold,
 mind fuzzy, not diagnosing the shell problem further).



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