Redirection of stdout and stderr to the same file
By Dmitry Kabanov
Often, when running a program in console, one needs to save all its output
to a file.
Very often, such a program writes both to the standard output (stdout
)
and to the standard error output (stderr
).
For example, by default, in a Python program the print
function writes to
stdout
and the functions of the logging
module to stderr
.
Therefore, one needs to redirect both stdout
and stderr
to the same file.
Redirection basics
Redirection of stdout
is done via >
syntax:
cmd >log.txt
and redirection of stderr
to stdout
is done with 2>&1
.
Thus, one just needs to combine these two redirections together
to redirect stderr to stdout
and then redirect both to log.txt
.
However, the order of redirection instructions is important here!
Wrong way
This command:
cmd 2>&1 >log.txt # wrong!
writes stderr
to stdout
but separately writes stdout
to file log.txt
.
If stdout
is connected to screen,
then stderr
effectively will be written to screen but stdout
to file.
Why this happens?
The shell executes these redirection instructions immediately,
hence, when 2>&1
redirection is implemented, it is not yet known
that stdout
will be redirected to log.txt
in the next instruction.
Right way
The right way to redirection is the command
cmd >log.txt 2>&1 # right!
that will write both stderr
and stdout
to the same file file.txt
.
As a shortcut for the previous command, one can use
cmd &>log.txt
This shortcut is not only shorter but also allows to avoid the possibility of a bug by using the wrong order.