Redirection quirks: 2>&1 >file -- vs. -- >file 2>&1

John Mackin john at basser.oz
Wed Jan 8 08:28:14 AEST 1986


In article <350 at bdaemon.UUCP> carl at bdaemon.UUCP (Carl Brandauer) writes:

> The real explanation for all of this is that once a Bourne shell command
> line has been read (up to \n or ; or & etc.) it is evaluated from right to
> left.

Incorrect.  In the case you gave, evaluation may indeed proceed
from right to left; the order of evaulation is not documented.
But if you examine the original posting, you will see the statement

>    $ command 2>&1 >file
>    $ command >file 2>&1
> The 2>&1 in the first line redirects unit 2 to the tty, not to the file.

This statement is *correct*.  The redirections are evaluated from
LEFT to RIGHT, not from right to left.  In the (Eighth Edition)
sh(1) manual entry on our system, this behavior is documented:

	The order in which redirections are specified is
	significant.  The shell evaluates redirections left-to-
	right.  For example:

	     ... 1>xxx 2>&1

	first associates file descriptor 1 with file xxx.  It
	associates file descriptor 2 with the file associated with
	file descriptor 1 (i.e. xxx).  If the order of redirections
	were reversed, file descriptor 2 would be associated with
	the terminal (assuming file descriptor 1 had been) and file
	descriptor 1 would be associated with file xxx.

John Mackin, Basser Department of Computer Science,
	     University of Sydney, Sydney, Australia

seismo!munnari!basser.oz!john	john%basser.oz at SEISMO.CSS.GOV



More information about the Comp.unix.wizards mailing list