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

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



On Wed, Nov 05, 2008 at 09:20:36PM +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.
> 
> It's OK when declaring the function as
> 
> foo() echo a > file
> or
> foo() (echo a) > file
> 
> instead of
> 
> foo() { echo a; } > file
[...]

Interestingly, foo() > file { echo a; } works:

~$ bash -c 'f() { echo a; } > /dev/null; declare -f'
f ()
{
    echo a
} > /dev/null
~$ ksh -c 'f() { echo a; } > /dev/null; typeset -f'
f() { echo a; } > /dev/null;%                                                                                                                                                        ~$ zsh -c 'f() { echo a; } > /dev/null; typeset -f'
f () {
        echo a
}
~$ zsh -c 'f() > /dev/null { echo a; }; typeset -f'
f () {
        {
                echo a
        } > /dev/null
}


-- 
Stéphane



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