← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 9 of 12

Debug Mode (set -x)

In this page:

  1. Debug Mode (set -x)

Debug Mode (set -x)

set -x makes Bash print each command with its expanded arguments before running it, which is invaluable for tracing exactly what a script does. set +x turns it back off. Debug tracing is usually enabled temporarily around a suspicious section rather than for an entire large script.

Note: Wrap only the suspicious section in set -x / set +x rather than tracing the whole script, to keep the output readable.

Example: Debug Mode (set -x)

bash
#!/bin/bash
echo "Before debug mode"

set -x
x=5
y=$((x * 2))
set +x

echo "After debug mode, y=$y"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.