I'm trying to learn the effect of exec > >(tee logfile)
in the following code:
#!/bin/bash
exec 7>&1 # save stdout for later reset
exec > >(tee logfile) # point stdout to FIFO pointing to tee command?
#+expect any subsequent output to stdout to be sent
#+both to stdout and logfile.
# so let's send some output
echo
echo howdy # expect these strings to be sent both to terminal and
echo g\'day #+logfile
echo hello!
# restore stdout
exec 1>&7
exec 7>&-
#check content of logfile!
echo ------------
cat logfile
I'm just guessing here that exec > >(tee logfile)
will redirect stdout to >(tee logfile)
.
Here's the output to terminal when this script is run:
--------------------
howdy
g'day
hello!
howdy
g'day
hello!
And here's the content of logfile:
howdy
g'day
hello!
It seems my attempt to redirect stdout back to terminal has no effect: exec 1>&7
. Perhaps, exec 1>&7
happens AFTER logfile has been written and its content sent to terminal.
And I don't understand the output to terminal when the script is executed. I'm guessing exec > >(tee logfile)
is blocked until cat logfile
reads it. Then content of logfile is duplicated to terminal due to tee logfile
.
Could you help me understand these points?
Thanks.
Best Answer
The general form of the commmand is
exec > output
which causes all further output to stdout to be sent to the file "output".This can be extended; eg
exec 2> error
will cause all futher output to stderr to be sent to the file "error"Now,
>(...)
is a bashism that means write the output to a command; in this case the command is "tee logfile"So we add the two together.
exec > >(tee logfile)
means "write all further output to the commandtee logfile
".Which means that all future output will be sent to the screen and (via
tee
) to the file "logfile"