Bash Debugger

Build-in Debugging Features

The simplest way is to activate debugging output with the -x command line option or in the script with set -x. This will print every line to stderr before it is executed. In those lines, the variables are already replaced with their values, therefore it's easier to see what's happening. Combined with the option -v that prints each line as it was read from the script, a malfunction can often be traced back to the line where it happened and the variables can be analyzed.

The fourth prompt PS4 allows to set the marker for -x tracing lines:

export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '

or, extended with colors:

export PS4='+\[\033[1;33m\]${BASH_SOURCE}\[\033[0m\]:\[\033[0;31m\]${LINENO}\[\033[0m\]:\[\033[1;34m\]${FUNCNAME[0]}\[\033[0m\]: '

If you're debugging a larger script, you can activate tracing for only parts of the script:

N=4
set -x      # start tracing
N=$(( N * 2 ))
set +x      # stop tracing
echo $N

BashDB

The BashDB is a wrapper that executes shell scripts with debugging features such as breakpoints, inspecting variables and changing the script under test.

See also

social