O padrão POSIX especifica que o redirecionamento de shell é da esquerda para a direita; ou seja, a ordem é significativa:
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 filefoo
.
bash
opera em conformidade com esta parte da norma:
$ 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
$
Quanto à tubulação:
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 ofcommand2
.