Is the following snippet valid Bourne shell code?

The following snippet triggers a syntax error when interpreting bash 4.3.39 (1), but not zsh 5.0.5.

eprintf()
{
    1>&2 {
        printf "$@"
        printf '\n'
    }
}

I just found an error in bash syntax ?

+4
source share
2 answers

It's not a mistake; from the man page:

The following redirection operators may precede or appear anywhere within the command simple or may follow the command.

So,

>/dev/null echo foo
echo >/dev/null foo
echo foo >/dev/null

all legal and equivalent, but redirection should follow a composite team {...}.

{ bash (alias {=foo { foo), 1>&2 { , { , eprintf . } , , .

, bash zsh , , bash.

+3

Single Unix Specification, Shell Utilities Volume :

fname() compound-command[io-redirect ...]

:

eprintf() { printf "$@" ; printf '\n' } >&2

, , ({ ... }) io, , .

, , .

+1

All Articles