I know how to use tee
to write the output (STDOUT
) of aaa.sh
to bbb.out
, while still displaying it in the terminal:
./aaa.sh | tee bbb.out
How would I now also write STDERR
to a file named ccc.out
, while still having it displayed?
bashlinuxteeunix
I know how to use tee
to write the output (STDOUT
) of aaa.sh
to bbb.out
, while still displaying it in the terminal:
./aaa.sh | tee bbb.out
How would I now also write STDERR
to a file named ccc.out
, while still having it displayed?
Best Answer
I'm assuming you want to still see STDERR and STDOUT on the terminal. You could go for Josh Kelley's answer, but I find keeping a
tail
around in the background which outputs your log file very hackish and cludgy. Notice how you need to keep an exra FD and do cleanup afterward by killing it and technically should be doing that in atrap '...' EXIT
.There is a better way to do this, and you've already discovered it:
tee
.Only, instead of just using it for your stdout, have a tee for stdout and one for stderr. How will you accomplish this? Process substitution and file redirection:
Let's split it up and explain:
>(...)
(process substitution) creates a FIFO and letstee
listen on it. Then, it uses>
(file redirection) to redirect the STDOUT ofcommand
to the FIFO that your firsttee
is listening on.Same thing for the second:
We use process substitution again to make a
tee
process that reads from STDIN and dumps it intostderr.log
.tee
outputs its input back on STDOUT, but since its input is our STDERR, we want to redirecttee
's STDOUT to our STDERR again. Then we use file redirection to redirectcommand
's STDERR to the FIFO's input (tee
's STDIN).See http://mywiki.wooledge.org/BashGuide/InputAndOutput
Process substitution is one of those really lovely things you get as a bonus of choosing
bash
as your shell as opposed tosh
(POSIX or Bourne).In
sh
, you'd have to do things manually: