How do I get both STDOUT and STDERR to go to the terminal and a log file?

Use “tee” to redirect to a file and the screen. Depending on the shell you use, you first have to redirect stderr to stdout using

./a.out 2>&1 | tee output

or

./a.out |& tee output

In csh, there is a built-in command called “script” that will capture everything that goes to the screen to a file. You start it by typing “script”, then doing whatever it is you want to capture, then hit control-D to close the script file. I don’t know of an equivalent for sh/bash/ksh.

Also, since you have indicated that these are your own sh scripts that you can modify, you can do the redirection internally by surrounding the whole script with braces or brackets, like

#!/bin/sh
{
    ... whatever you had in your script before
} 2>&1 | tee output.file

Leave a Comment