On this SO thread and a few other threads I have seen the following commands for redirecting stdout
and stderr
to a file.
Are they all equivalent? Is there any difference between them?
command1 >> logfile 2>&1
command &> logfile
command >& logfile
Best Answer
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 redirectstderr
tostdout
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
andzsh
But:
Only
>&
works incsh
ortcsh
In
ksh
only2>&1
works.I hate
ksh
. While>&
just gave an error, the&>
backgrounded a part of the command and emptied the logfile (if non-empty).