3

I want to write stdout to a file but also prints stdout and stderr. I tried using tee:

prog | tee stdout.txt

but this causes the printed stderr and stdout to be interleaved incorrectly, i.e. if the correct output should be

OUT1 ERR1
OUT2 ERR2
OUT3 ERR3
OUT4 ERR4

using tee might print them out of order, e.g.:

ERR1 ERR2
OUT1
OUT2 
OUT3 ERR3 
OUT4
ERR4

Is there any way to print both stdout and stderr to screen and also write the stdout to a file without gobbling up the printed output?

1

I would forward stdout to a file, then use tail to see the file content as it is written. That means you need to have two terminals open.

Terminal one:

prog > stdout.txt

Terminal two:

tail -f stdout.txt

So, in terminal two you will see stdout content as it is written to stdout.txt, and in terminal one you will see stderr. You can also forward both stdout and stderr to a file, and do the same thing. In that case you need to use prog >& outerr.txt. (This will work on tcsh, but you can do it in bash, too.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.