Debug Bash Shell Scripts

Howto test and debug bash shell scripts

Testing Bash Shell Scripts


Sometimes when you are creating a new script, you may encounter a few problems. Thankfully Bash can help with its built in debugging features which allow you to track the cause of the problem quickly.



Running your script in Debug Mode


One of the most common ways to debug a Bash script is to use the built in debug mode. This mode can be activated simply by starting your shell with the "-x" option:

Running in debug mode: bash -x script.sh



john@john-desktop:~/scripts$ bash -x array1.sh
+ test=(one two three four five)
+ echo one
one
+ echo two
two
+ echo three
three
+ echo four
four
+ echo five
five
+ echo one two three four five
one two three four five

Each line of the script is displayed with any associated values that have been assigned or expanded. The above debugging method applies to the whole script. Quite often you probably only want to test a specific section of your script. This can be achieved using the "set -x" and "set +x" options:

set -x : Activates debugging from the current position within script.

set +x : Deactivates debugging from the current position within script.

For example, we could debug the following script in a specified section as follows:

Activating Debugging within a specified section of a script



#!/bin/bash
#
# Debug Example
#
test=(one two three four five)

echo ${test[0]}
echo ${test[1]}
echo ${test[2]}
set -x
echo ${test[3]}
set +x
echo ${test[4]}

echo ${test[*]}

In the above script, we have activated the debugging mode around the "echo ${test[3]}" statement.

Output from script with debugging mode activated



john@john-desktop:~/scripts$ ./debug2.sh 
one
two
three
+ echo four
four
+ set +x
five
one two three four five

We can now see that the debug mode was activated for the specified section only. You can activate and deactivate debugging as many times as needed within your script.


Debug Parameter Description
set -f Disables globbing (filename expansion)
set -v Verbose mode, displays shell input lines as they are read
set -x Activates command line tracing

Debugging -v


Using the "-v" option allows you to easily see what line of code is being executed as your script executes.

debug using -v example:



john@john-desktop:~/scripts$ bash -v debug2.sh 
#!/bin/bash
#
# Array Example
#
test=(one two three four five)
echo ${test[0]}
one
echo ${test[1]}
two
echo ${test[2]}
three
echo ${test[3]}
four
echo ${test[4]}
five

echo ${test[*]}
one two three four five

Like the "-x" option, you can use the "set -v" and "set +v" within your script to debug specific sections only.



Combining Debugging Options


As we have seen we can specify the debug options throughout our code or we can issue the debug option directly when we execute out script. However, we can also specify combinations of debug modes on the first line of our script. This line is generally known as "shebang" or the "hash bang" line. This line is used to specify which shell/interpreter to use when executing our scripts.

Debugging options on first line



#!/bin/bash -xv

Output from Script when run with -xv debugging options



john@john-desktop:~/scripts$ ./debug2.sh 
#!/bin/bash -xv
#
# Debug Example
#
test=(one two three four five)
+ test=(one two three four five)
echo ${test[0]}
+ echo one
one
echo ${test[1]}
+ echo two
two
echo ${test[2]}
+ echo three
three
echo ${test[3]}
+ echo four
four
echo ${test[4]}
+ echo five
five

echo ${test[*]}
+ echo one two three four five
one two three four five

Using Vim editor with Colour Highlighting


One of the most popular text editors available on Linux distributions is an editor called "VIM". Vim is basically a feature enhanced version of the famous "vi" text editor. One very useful feature of "Vim" is that it allows you to use syntax highlighting. This functionality can be very useful when debugging a large script. To activate syntax highlighting we can issue the following command sequences from the text editor. (Escape=press escape key)

Escape:syntax on - This key sequence is used to activate the colour syntax high-lighting mode.

Escape:syntax off - This key sequence is used to disable the colour high-lighting mode.

Below are some example screen shots of the Vim editor (Version 7.3.429) being used on an Ubuntu Desktop.


Turn VIM high-lighting on (syntax on)


Bash Scripting - Vim Syntax on

Turn VIM high-lighting off (syntax off)


Bash Scripting - Vim Syntax off