Debugging Bash Shell Scripts Techniques

Although Bash scripting is not a full fledged compile-able programming language, it is still very powerful and the script size can grow to enormous size. Therefore, even when you are just occasional bash scripting user or complete beginner some techniques is good to know in order to help debug you bash code. In this article we list some most command and basic bash scripting debbuging techniques.

The first debugging technique is bu using -x bash option during script execution. This will print all lines of code that is execute. For an illustration let’s consider a following example:

#!/bin/bash 

echo hello

myvar=3
echo $myvar

To debug this bash shell script use -x bash option during script execution:

$ bash -x bash-script.sh 
+ echo hello
hello
+ myvar=3
+ echo 3
3

Next and very common bash scripting debugging is to use echo command. Place echo command on each line to print variable content or even entire line of code to see what is being execute and what follows. This has already been illustrated in the above code example echo $myvar.