Redirection differences between &> >& and 2>&1
Since you have tagged zsh
, let me tell you that all the 3 redirections works exactly the same way. As you might have read in both the duplicate posts (the one in the comment and the one in your post), they all redirect stderr
to stdout
which inturn is redirected to the file 'logfile' (ie, the logfile will contain both the output and errors).
But their behaviour changes a LOT depending on the shell you are in.
All the three styles of redirections works well in the same way in bash
and zsh
But:
Only >&
works in csh
or tcsh
[soum@server ~]$ ./test.sh > logfile 2>&1
Ambiguous output redirect.
[soum@server ~]$ ./test.sh &> logfile
Invalid null command.
[soum@server ~]$ ./test.sh >& logfile
[soum@server ~]$ echo $SHELL
/bin/tcsh
[soum@server ~]$
In ksh
only 2>&1
works.
$ ./test.sh >& logfile
-ksh: logfile: bad file unit number
$ ./test.sh &> logfile
[1] 23039
$ 1 2 3 4 5 6 logfile test.sh
ls: cannot access ttr: No such file or directory
[1] + Done(2) ./test.sh &> logfile
I hate ksh
. While >&
just gave an error, the &>
backgrounded a part of the command and emptied the logfile (if non-empty).