Redirect stderr to terminal and also file in bash

Answer #1 100 %

Yes, the tee command allows you to direct output to one or more files, as well as stdout.

As you pointed out in a comment, this doesn't work by itself because of the exec command.

This should do what you want:

exec 3>&1 1>test.log
echo "Check if this line is going to test.log"
exec 1>&3 3>&-
echo "Maybe this should go to stderr" | tee -a test.log >> /dev/stderr

I got the information about the way to restore stderr by properly setting up the original exec, from here, and combined it with tee.

You’ll also like:

© 2023 CodeForDev.com -