Precedence of stdin and stdout redirection in Bash

The POSIX standard specifies that shell redirection is from left to right; that is, order is significant:

The construct 2>&1 is often used to redirect standard error to the same file as standard output. Since the redirections take place beginning to end, the order of redirections is significant. For example:

ls > foo 2>&1

directs both standard output and standard error to file foo. However:

ls 2>&1 > foo

only directs standard output to file foo because standard error was duplicated as standard output before standard output was directed to file foo.

bash operates in compliance with this part of the standard:

$ ls doesnotexist > foo 2>&1
$ cat foo
ls: cannot access doesnotexist: No such file or directory
$ ls doesnotexist 2>&1 > foo
ls: cannot access doesnotexist: No such file or directory
$ cat foo
$ 

As for piping:

Because pipeline assignment of standard input or standard output or both takes place before redirection, it can be modified by redirection. For example:

$ command1 2>&1 | command2

sends both the standard output and standard error of command1 to the standard input of command2.


I guess neither. A pair of parenthesis means a sub-shell. But in this case, no sub-shell will be started because of the redirection. Bash simply feeds cmd2 into stdin and feeds stdout into cmd3.

I'm thinking, do you mean something like cmd1 | cmd2 | cmd3? Because your cmd2 and cmd3 are usually normal files instead of "cmds".