http://stackoverflow.com/questions/818255/in-the-shell-what-is-21
2 is the default file descriptor for stderr.
1 is the default file descriptor for stdout.
>&
is shell syntax for "fold a file descriptor into another"
Here is one way to remember this construct (altough it is not entirely accurate): at first, 2>1
may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1
". &
indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1
.
cmd 2>&1 >>file
does not redirect stderr to the file, but cmd >> file 2>&1
does. Order matters. In the first case, stderr is redirected to the stdout of the shell (possibly a tty if the command is entered interactively), and then stdout is directed to the file. In the second case, stdout is directed to the file, and then stderr is directed to the same place.