Bash:
Using a debugger

How to:

Bash doesn’t come with a built-in debugger like some other languages, but you can use built-in commands like set -x to trace what’s happening. Or, for an upgrade, there’s bashdb, a proper debugger to step through your code. Here’s a peek:

# Using set -x to debug
set -x
echo "Start debugging"
my_var="Hello, Debugging World!"
echo $my_var
set +x

# Using bashdb
# Install bashdb with your package manager, e.g., apt, yum, brew.
# Debug a script called my_script.sh:
bashdb my_script.sh

Output when running with set -x:

+ echo 'Start debugging'
Start debugging
+ my_var='Hello, Debugging World!'
+ echo 'Hello, Debugging World!'
Hello, Debugging World!
+ set +x

Deep Dive

Historically, debugging Bash scripts meant littering your code with echo statements. But then came set -x, giving us a peek into the runtime execution without manual printouts. And for those craving more control, the bashdb debugger popped up, inspired by the gdb debugger for C/C++.

As for alternatives, beyond the set commands (-x, -v, -e), other options include redirecting output to a file for analysis or using external tools like ShellCheck for static analysis.

Implementation-wise, set -x is easy; it’s a native Bash option that prints commands and their arguments as they’re executed. bashdb, on the other hand, allows stepping through code, setting breakpoints, and evaluating expressions - things that give you a fighting chance against more elusive bugs.

See Also