I would like to save the stderr stream of a command into a log file but I also want to display the whole output (stdout + stderr) on the screen. How can I do this?
I only found the solution to display stdout + stderr to the console and redirect both streams to a file as well:
foo | tee output.file
(https://stackoverflow.com/questions/418896/how-to-redirect-output-to-a-file-and-stdout)
But I only want to redirect stderr to the log file.
Best Answer
With a recent bash, you can use process substitution.
This just sends stderr to a program running tee.
More portably
This makes file descriptor 3 be a copy of the current stdout (i.e. the screen), then sets up the pipe and runs
foo 2>&1 >&3
. This sends the stderr of foo to the same place as the current stdout, which is the pipe, then sends the stdout to fd 3, the original output. The pipe feeds the original stderr of foo to tee, which saves it in a file and sends it to the screen.