Is there a way to send the information displayed by running a bash script with the -x option to a file, while not changing the standard output seen by a user running the script?
This is a debugging feature I would like to implement in a bash script we use that changes frequently.
Much appreciated.
The output from -x goes to stderr, not stdout. But even that can be a problem -- plenty of scripts will have functional dependencies on the content of stderr, and its kind of messy to have the debug and stderr streams mixed together in some cases.
Bash versions > 4.1 do offer a different solution: the BASH_XTRACEFD environment variable allows you to specify a file descriptor which will be used to send the debug stream to. This can be a file or pipe or any other unix-y goodness you like.
With a bit more fiddling, you can do other things -- like doing a 'tee' on the stdout and/or stdin streams, and interleave those with the debug output, so your log is more complete. For more details on this, see https://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself.
The big advantage of this approach over alternatives is that you're not risking changes to the behavior of your script by injecting debug output into stdout or stderr.
set -x
doesn't send anything to standard output, so there's no problem there. What it does do is write to standard error, which goes to the console by default.What you want to do is redirect stdout to another file, like this:
See
man tee
.You run it like
tee commandname filename
and it will display the commands output tostdout
and write it also intofilename
.Problem Statement:
Display the output of the script on the screen and redirect output to a file.
Resolution:
Use:
exec &> >(tee logfile)
Tee: tee is a bash command to redirect the output to a file as well as display it on the screen.
set -x: If you do not want to log the script commands in the log file (apart from the command output), you can remove set -X from the script below.
tee
command has been already mentioned but I use another alternative to merge all the outputs in the trace log. It eases the debugging, in my opinion.The tip is to use
/dev/tty
with thetee
command because you cannot reuse stdout/stderr filehandles anymore since they are already redirected to your trace log.Try this:
Note: you can read synchronously with
tail -f trace.log
in another term.