I must admit, I'm not completely sure what's your goal... however, the correct synat for your exec
-trick should be as follows (you only missed a >
):
exec > >(tee -a logfile) 2>&1
A simple demonstration:
% cat test #!/bin/bash echo stdout not logged echo stderr not logged >&2 exec > >(tee -a logfile) 2>&1 echo stdout logged echo stderr logged >&2 % bash test stdout not logged stderr not logged % stdout logged stderr logged % cat logfile stdout logged stderr logged %
You can see, the exec
messes up the output by printing the prompt again, when the script reaches that line. So why don't you just log e.g. stdout
, but not stderr
of your script?
% (echo stdout; echo stderr >&2) | tee logfile stderr stdout % cat logfile stdout