Frequently Asked Question

What does 2>&1 mean and why does the order matter?

2>&1 means "make file descriptor 2 point to wherever file descriptor 1 is currently pointing". The & distinguishes it from 2>1, which would redirect stderr to a file literally named 1. With & the shell knows you are talking about another file descriptor, not a filename.

Order is critical because redirections are applied left to right. cmd > out.log 2>&1 first points fd 1 at out.log, then makes fd 2 also point at out.log; so both streams end up in the same file. But cmd 2>&1 > out.log makes fd 2 point at wherever fd 1 was before it got redirected (still the terminal), and then sends fd 1 to the file. Stderr keeps going to the screen and stdout goes to the file. The two pipelines look almost identical and behave entirely differently, which is why every shell tutorial spends extra time on this.

Further reading and video