Why does 2>&1 need to come before a | (pipe) but after a "> myfile" (redirect to file)?

Viewed 5218

When combining stderr with stdout, why does 2>&1 need to come before a | (pipe) but after a > myfile (redirect to file)?

To redirect stderr to stdout for file output:

  echo > myfile 2>&1

To redirect stderr to stdout for a pipe:

  echo 2>&1 | less



My assumption was that I could just do:

  echo | less 2>&1 

and it would work, but it doesn't. Why not?

3 Answers
Related