$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

Debugging with set -x and PS4

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:22

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.

BASH4 lines
set -x
name="Ada"
echo "hello $name"
set +x

The 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.

BASH1 lines
bash -x -c 'a=5; echo "value $a"' 2>&1

The 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.

BASH6 lines
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.

Tip

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.

BASH4 lines
v="two words"
set -x
printf "%s\n" $v
set +x

The 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.

BASH1 lines
bash -x -c 'v="a  b"; printf "[%s]\n" $v; printf "[%s]\n" "$v"' 2>&1

The 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.

Terminal lab · 2 steps · real Linux in your browser

Trace a script with PS4 line numbers

Write a script that traces itself with line numbers, then record a one-line trace in a file.

01
Trace with line numbers
Create trace.sh in /workspace. Set PS4 to a prefix that includes LINENO, turn on tracing, run an assignment and an echo, and turn tracing off: ``bash cat > trace.sh <<'EOF' PS4='+ line ${LINENO}: ' set -x x=1 echo "x=$x" set +x EOF `` Run it with its trace sent to a file: bash trace.sh 2> trace.log.
Show hint for step 1
The trace is written to standard error, so redirect standard error to trace.log. The prefix must come from PS4.
02
Trace a single command
Run one command with tracing and send its trace to xtrace.log in /workspace. The command runs a shell that assigns a and echoes it: ``bash bash -x -c 'a=5; echo $a' 2> xtrace.log `` Open xtrace.log and find the line that shows the assignment.
Show hint for step 2
The -x option belongs to the bash command, and the trace goes to standard error.
Check yourself Question 1 of 4
Concept Check Which option traces commands recall

Which set option prints each command after expansion to standard error?

Recap & Key Takeaways

set -x prints each command after expansion to standard error, which shows the real arguments and the order of execution.
bash -x runs a whole script or bash -x -c runs one command with tracing, which avoids editing the file.
PS4 controls the trace prefix, and placing LINENO in it with single quotes adds line numbers to every traced line.
Read a trace for quoting problems first: an unquoted value that appears as several words shows where word splitting happened.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue