In bash
, this:
exec &> >(tee -a "$LOG_FILE")
Will redirect stderr
& stdout
to tee
that allows the output to be displayed on the terminal while also logged (written to $LOG_FILE
), but it uses process substitution, a feature not available in sh
If I run the script with sh:
sh script
I get:
syntax error near unexpected token `>'
In my attempt to have the same result in sh
, I tried:
exec 2>&1 | tee -a "$LOG_FILE"
But I notice that nothing gets written to $LOG_FILE
, instead I had to use:
exec > "$LOG_FILE" 2>&1
That writes the log to the file, but I don't see any output when running the script.
Any ideas?
From the accepted answer, the usage of:
mkfifo "$HOME/.pipe.$$"
helps/complements the suggested answer, since allows to have the code inline and not within a block
sh
though? in my system it's a symlink todash
, what is it in yours? – Jaromanda X May 09 '23 at 07:15/usr/bin/sh
– nbari May 09 '23 at 07:22ls -l /usr/bin/sh
. For me it is:/usr/bin/sh -> dash
. – Stewart May 09 '23 at 07:30/usr/bin/sh
is usually a symlink, what is it linked to – Jaromanda X May 09 '23 at 08:05/usr/bin/sh -> bash
(testing on a red-hat 8) – nbari May 10 '23 at 09:20