I know how to redirect stdout to a file:
exec > foo.log
echo test
this will put the ‘test’ into the foo.log file.
Now I want to redirect the output into the log file AND keep it on stdout
i.e. it can be done trivially from outside the script:
script | tee foo.log
but I want to do declare it within the script itself
I tried
exec | tee foo.log
but it didn’t work.
Note that this is
bash, notsh. If you invoke the script withsh myscript.sh, you will get an error along the lines ofsyntax error near unexpected token '>'.If you are working with signal traps, you might want to use the
tee -ioption to avoid disruption of the output if a signal occurs. (Thanks to JamesThomasMoon1979 for the comment.)Tools that change their output depending on whether they write to a pipe or a terminal (
lsusing colors and columnized output, for example) will detect the above construct as meaning that they output to a pipe.There are options to enforce the colorizing / columnizing (e.g.
ls -C --color=always). Note that this will result in the color codes being written to the logfile as well, making it less readable.