I'm converting a script from bash to zsh and have been floundering for days with the output. I want the stdout and stderr to both go to both the console and a log file. I've tried everything I can find online but no joy. I've read the redirection section of the zsh manual but found nothing that helped, although I must admit it is unclear to me.
Here are the things I've tried near the beginning of the script, experimentally, and their result:
exec 2>&1 | tee -i test.log # everything in console, log created but empty
exec 2>&1 | tee -i > test.log # everything in console, log created but empty
exec 1>>$LOG; exec 2>&1 # everything in log, none in console
exec 2>&1; exec 1>>$LOG # console gets errors only; log stdout only
exec &> >(tee "$LOG") # works in bash, error in zsh
exec > >(tee -i ${LOG?}) 2>&1 # error
exec |& tee $LOG # error
exec > >(tee $LOG) 2>&1 # error
I also tried to understand the zsh option multios, but couldn't make much sense of it. It's supposed to be on by default, but I don't find it in the list of options when I run setopt
. Nor when I run setopt multios
. When I run unsetopt multios
, I see the option nomultios
, but that doesn't seem to help
I understand that similar questions have been asked and answered, but the answers are mostly in the list above and have not worked for me.
I don't think it's relevant, but I also set up a debug log that gets file descriptor 3. It's just for debug output lines that are ended with >&3.
Note for anyone looking for such a solution: The above exec trials were done with a messed up shebang, and the results with that corrected were a bit different. In addition to the solution by Gairfowl, below, this also worked:
exec > >(tee $LOG) 2>&1
exec
that's used or did you replace your executable name with confusingexec
?