When you execute a command, you can redirect the standard output (known as /dev/stdout
) of the command directly to the file. Also if the command generates error-output (generally send to /dev/stderr
) you can also redirect it to a file as:
$ command > /path/to/output.txt 2> /path/to/error.txt
When you execute the command set -x
, you ask it to generate a trace of the commands being executed. It does this by sending messages to /dev/stderr
. In contrast to a normal command, you cannot easily redirect this in a similar way as with a normal command. This is because bash
executes the script and at the same time generates the trace to /dev/stderr
. So if you would like to catch the trace, you would have to redirect the error output of bash
directly. This can be done by the command
exec 2> /path/to/trace.txt
note: this will at the same time also contain all the error output of any command executed in the script.
Examples:
#!/usr/bin/env bash
set -x
command
This sends all output and error output to the terminal
#!/usr/bin/env bash
set -x
command 2> /path/to/command.err
This sends the output of command
and the trace of bash to the terminal but catches the error output of command
in a file
#!/usr/bin/env bash
set -x
exec 2> /path/to/trace.err
command 2> /path/to/command.err
This sends the output of command
to the terminal, the error output of command
to a file, and the trace of the script to /path/to/trace.err
#!/usr/bin/env bash
set -x
exec 2> /path/to/trace_and_command.err
command
This sends the output of command
to the terminal, the trace and the error of command
to a file.