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

POSIX and the "&>" operator



The following is part of an exchange on the austin-group mailing list.
The assertion is that "foo&>bar" has a well-defined semantics in POSIX
sh and therefore the bash extension (which zsh adopted) to make "&>" a
synonym for "2>&1 >" is in violation of POSIX compliance.

(I tried to send a follow-up to the austin-group about it, but I can't
figure out with what email address they think I'm subscribed, and the
list won't accept mail from non-subscribers.)

This is interesting for two reasons:
(1) I suspect that when zsh is emulating a POSIX shell, it should avoid
treating "&>" as a token and instead parse it as "&" ">"
(2) I'm puzzled by Korn's assertion that some syntax like "&|" is an
"invalid extension", as opposed for example to ">;"


---------- Forwarded message ----------
Date: Thu, 5 Feb 2009 12:19:52 -0500
From: Glenn Fowler <gsf@xxxxxxxxxxxxxxxx>
To: austin-group-l@xxxxxxxxxxxxx
Subject: shell redirection question
Resent-Date: 5 Feb 2009 17:20:33 -0000
Resent-From: austin-group-l@xxxxxxxxxxxxx
Resent-To: austin-group-l@xxxxxxxxxxxxx


in the posix shell grammar ';' and '&' are syntactically equivalent

so these two commands, modulo one being in the background,
should produce the same output ("stdout" to stdout, "stderr" to stderr):

sh -c "( echo stdout; echo stderr >&2 ) ;>/dev/null; wait"
sh -c "( echo stdout; echo stderr >&2 ) &>/dev/null; wait"

ksh produces the same output for both cases
i.e., it treats ';' and '&' as command separators

bash, even with --posix, and zsh, treat the '&>/dev/null'
as '>/dev/null 2>&1'

is a shell implementation permitted to treat
'&>/dev/null' as '>/dev/null 2>&1' ?

-- Glenn Fowler -- AT&T Research, Florham Park NJ --


--- Forwarded mail from David Korn <dgk@xxxxxxxxxxxxxxxx>

Date: Thu, 5 Feb 2009 16:52:21 -0500
From: David Korn <dgk@xxxxxxxxxxxxxxxx>
To: austin-group-l@xxxxxxxxxxxxx, chet.ramey@xxxxxxxx
Subject:  Re: Re: shell redirection question

cc: chet.ramey@xxxxxxxx
Subject: Re: Re: shell redirection question
--------

> Chet Ramey <chet.ramey@xxxxxxxx> writes:
> 
> >> is a shell implementation permitted to treat
> >> '&>/dev/null' as '>/dev/null 2>&1' ?
> >
> > This is a documented bash extension.  It's not posix.  As such, it's
> > probably not appropriate for the group to decide.
> 
> Certainly it's not appropriate for the group to decide what extensions
> Bash should make, but here it seems clear that Bash and zsh do not
> conform to the current POSIX spec here.  Either the POSIX spec should
> change, or bash (with --posix) should change.  I prefer the former,
> that is, I suggest modifying POSIX so that "&>" leads to
> implementation-defined behavior.
> 
> 

Of course the group cann't say what extensions any shell should make,
but they can indicate whether an extension is compatible with posix.

When you run ksh93 with the -n option, it produces a warning whenever
it finds &< without a space in between.  The only time I have ever
seen this warning is when a script intended for bash was run so
I would say that &< in any other context is exceedingly rare.

Excluding the ( and ) operators, I have a list below of all the other
two character operator characters and how they are used.

The wording in the standard under token recognition, says, "If the
previous character was uses as part of an operator and the current
character is not quoted and can be used with the current characters
to form an operator, it should be used as part of that (operator)
token."

The grammar section also list which operators are defined double character
operators.  However, the standard doesn't state how extensions
should be made.  ksh93 has used the rule that if the second character
would generate a syntax error, then it is a valid extension.

Maybe, ;<, ;>, &< and &> should produce unspecified behavior without
an interviening space so that these can be used as an extension.
Minimally &> is commonly used in bash so this might be considered
a legal extension.

<<	used by posix
<>	used by posix
<;	valid extension
<|	valid extension
<&	used by posix
><	valid extension
>>	used by posix
>;	valid extension used by ksh93
>|	used by posix
>&	used by posix
;<	invalid extension
;>	invalid extension
;;	used by posix
;|	valid extension
;&	valid extension used by ksh93
|<	invalid extension
|>	invalid extension
|;	valid extension
||	used by posix
|&	valid extension used by ksh93
&<	invalid extension
&>	invalid extension used by bash
&;	valid extension
&|	invalid extension
&&	used by posix

ksh93 also uses <# and ># which are valid extensions.
	

David Korn
dgk@xxxxxxxxxxxxxxxx


---End of forwarded mail from David Korn <dgk@xxxxxxxxxxxxxxxx>

-- 



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