The following snippet triggers a syntax error when interpreted by bash 4.3.39(1) but not by zsh 5.0.5.
eprintf()
{
1>&2 {
printf "$@"
printf '\n'
}
}
Have I just found a bug in bash parser?
The following snippet triggers a syntax error when interpreted by bash 4.3.39(1) but not by zsh 5.0.5.
eprintf()
{
1>&2 {
printf "$@"
printf '\n'
}
}
Have I just found a bug in bash parser?
According to the Single Unix Specification, Shell and Utilities Volume, the function definition command syntax is:
fname() compound-command[io-redirect ...]
In your case that would give:
eprintf() { printf "$@" ; printf '\n' } >&2
The function definition command syntax only allows for one compound command, moreover, compound commands ( { ... }
) do not have any io redirection mentioned in the syntax, functions do.
So according to my reading of the standard, this really doesn't look much like a bug.
It's not a bug; from the man page:
So
are all legal and equivalent, but the redirection must follow the compound command
{...}
.Because
{
has no real special meaning tobash
(alias {=foo
makes{
and alias forfoo
), the line1>&2 {
is parsed as a simple command, with the assumption that the lookup for{
will succeed wheneprintf
is eventually called. As a result, the first}
closes the compound statement serving as the body of the function, and the second one is unmatched and triggers the syntax error.I couldn't say without further research why
bash
andzsh
differ here, only that it is documented behavior forbash
.