How do I redirect both stderr and stout to multiple locations? How do I redirect both stderr and stout to multiple locations? unix unix

How do I redirect both stderr and stout to multiple locations?


result=`./command 2>&1 | tee output.log | tee /dev/tty`

[edit]

As n.m. points out in a comment, tee accepts multiple arguments:

result=`./command 2>&1 | tee output.log /dev/tty`

[second edit]

Borrowing an idea from Chris in the comments, you can also do this to send the output to stderr:

result=`./command 2>&1 | tee /tmp/foo.log >(cat 1>&2)`

To do exactly what you want, the best I have found is this:

exec 3>&1 ; result=`./command 2>&1 | tee /tmp/foo.log >(cat 1>&3)` ; exec 1>&3

(The whole problem here is that the backticks redirect stdout before anything inside gets to execute. So this line saves and restores the old stdout as descriptor 3, which may or may not be a good idea...)