01 / JUST GIVE ME THE ANSWER
The quick answer
File descriptor 1 is stdout and 2 is stderr. The expression 2>&1 duplicates the current destination of stdout onto stderr.
command >out.log 2>&102 / EXPLAIN IT PROPERLY
What's going on?
File descriptor 1 is stdout and 2 is stderr. The expression 2>&1 duplicates the current destination of stdout onto stderr.
Understanding the underlying mechanism makes troubleshooting much easier than memorising a command in isolation.
03 / UNDER THE BONNET
Dig a little deeper
Redirections are processed left to right. command >out 2>&1 sends both streams to out; command 2>&1 >out leaves stderr pointing at the original stdout.
04 / TRY IT YOURSELF
Hands-on commands
Try these in a suitable test environment. Read each command before running it, especially if you have elevated privileges.
printf 'hello\n' >out.log 2>&1
ls /no-such-path >out.log 2>&1
cat out.log05 / THE RABBIT HOLE
One more thing...
Redirection order matters more than punctuation suggests.
Explore another mystery ↗