how to get output of a script/program into a file?
The text that is displayed in the terminal comes from the stderr
stream (2). If you do just make > build_log.txt
, only the stdout
(1) stream is redirected to the build_log.txt
file.
-
stdout
is the standard output stream and has file descriptor number 1. This is the default stream being redirected in shells. -
stderr
is the standard error stream and has file descriptor number 2
To redirect the stderr
stream to that build_log.txt
file too, use:
make > build_log.txt 2>&1
-
make
is executed and- the
stdout
stream is redirected (>
) tobuild_log.txt
- the
stderr
stream is redirected (2>
) to thestdout
stream (&1
), which was redirected tobuild_log.txt
- the
The order is important, you cannot switch switch the redirection operators like make 2>&1 > build_log.txt
.
Alternative command:
make 2>&1 | tee build_log.txt > /dev/null
The redirection to /dev/null
is needed to hide output, tee
writes its input to build_log.txt
and outputs it too.