Linux – the difference between redirection 2>&1 1>file.txt and >a.txt 2>&1

linuxredirection

What is the difference between redirection ./prog 2>&1 1>file.txt and ./prog >a.txt 2>&1? It looks that ./prog 2>&1 1>file.txt doesn't save stderr to file but this ./prog >a.txt 2>&1 does.

Best Answer

Redirection operators are examined left-to-right, thus

 foo > bar 2>&1

first redirects standard-output to bar and then redirect standard-error to the location where standard-output is redirected at this point (thus bar, too).

 foo 2>&1 > bar

first redirects standard-error to what standard-output points to (most probably the terminal) and then redirects standard-output to bar.

Short: first one redirects both stdout and stderr to bar, second one redirects only stdout to bar and stderr to terminal.