Turning tracing on and off
set -x turns xtrace on for the rest of the script, and set +x turns it off again. The trace goes to standard error, so it does not mix with the normal output of the script. Each traced line begins with the prefix in the variable PS4, which defaults to a plus sign and a space.
set -x
name="Ada"
echo "hello $name"
set +xThe trace shows + name=Ada, then + echo hello Ada, and the output line hello Ada follows. The trace shows the words after expansion, so the quoted argument has become the separate words that echo received. The last command, set +x, is itself traced once before tracing stops.
Tracing one command
You do not have to change the script to trace it. Running bash -x script.sh traces every command from the start. For a single command, bash -x -c works the same way, which is useful for checking a pipeline or a quoted expression without editing a file.
bash -x -c 'a=5; echo "value $a"' 2>&1The trace includes + a=5 and then + echo value 5, followed by the output value 5. The redirection merges the trace into the same stream as the output so that you see the lines in order.
Customizing PS4 with line numbers
The default prefix tells you nothing about where a line came from. The variable PS4 is expanded for every traced line, so you can put LINENO in it, the current line number, and BASH_SOURCE for the file name. Use single quotes around the value so that the expansion happens at trace time, not when you set the variable.
PS4='+ line ${LINENO}: '
set -x
x=1
y=$((x + 1))
set +x
echo "y=$y"The trace lines now read + line 4: x=1 and + line 5: y=..., with the line numbers of the script. The line numbers make a long trace easy to match with the source. Because the trace depends on the variable being set before set -x, define PS4 first.
A common PS4 for scripts is + ${BASH_SOURCE[0]}:${LINENO}: ${FUNCNAME[0]:-main}: , which shows the file, line, and function. Keep it in your debugging wrapper rather than in production scripts, because the extra output is noisy.
Reading a trace
A trace shows the commands after expansion, so the first question to ask is whether the arguments are the ones you meant. Check quoting first, then values, then the order. Variables that were unset in your head often show up as empty words in the trace, and a word that should be one argument but appears as two points to a missing quote.
v="two words"
set -x
printf "%s\n" $v
set +xThe trace shows + printf %s\n two words, which reveals that the unquoted variable was split into two arguments. The printf then printed two lines. Quoting the variable would have kept the value together.
Keeping the trace out of the way
Trace output can be long. Two habits help. First, enable xtrace only around the code you are investigating, with set -x and set +x placed tightly. Second, send the trace to a file by redirecting the whole script with bash -x script.sh 2> trace.log, which keeps the terminal readable. The trace is still written to standard error, so this redirection captures it.
Hands-on: trace a quoting bug
Run the block below. It prints a value that contains a space, once unquoted and once quoted. Read the trace before you read the output.
bash -x -c 'v="a b"; printf "[%s]\n" $v; printf "[%s]\n" "$v"' 2>&1The trace shows the unquoted call with two words, and the quoted call with one. The lab for this unit asks you to capture a trace in a file and to set a PS4 that shows line numbers.