I know that I can save the output to a variable and then print it:
VAR=$(command)
echo "$VAR"
But this has a number of drawbacks:
- I won't see the progress of the command as it goes.
- In particular, will see all
stdout
output after allstderr
output, making it hard to match them.
- In particular, will see all
- Since this will result in no output for the duration of the command's work, in some environments (like Travis CI) this will terminate the job if the command works for long enough.
So, how can I save the output and also see it live on the console?
- Portable solutions are preferrable though a Linux/MacOS-only one will do in a pinch.
- A solution should not have undesirable side effects in
errexit
mode
From the top of my head, one can
tee
the output to an additional file descriptor set to the original stdout:One needs to have
set -o pipefail
set to detectcommand
's error inerrexit
mode.