I was finding a way to save all output to a file and print it.
And the command like the following does work perfectly!
ls "*" 2>&1 | tee ls.txt
But I think I don't understand it well.
And I tried ls "*" | tee ls.txt
. It doesn't work. The error message was not saved into ls.txt
.
Also I tried ls "*" 1>&2 | tee ls.txt
. It behaved some strange. What happened?
2>&1
says "redirect stderr (2) to wherever stdout (1) goes". 1
and 2
are the file descriptors of stdout
and stderr
respectively.
When you pipe ls
output to tee
, only stdout goes to tee
(without 2>&1
). Hence, the error messages are not saved into ls.txt
.