Debugging is an important part of programming in any language, and so it is the same for Bash, one of the most easy way to debug is to run the program line by line.

This way you can check what is happening with each line, and you can then find where is the error.

Debugging shell scripts

If you are using bash, you can debug using this command

bash -x <>

This way bash will print each line and then execute it, this will help you to find any possible error.


Given this shell script

echo 'This is an example'
df -h
free -m
echo 'this is the end'

If you run this with the -x parameter, you will have an output like this:

+ echo 'This is an example'
This is an example
+ df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   10M  172K  9.9M   2% /dev
/dev/sdb4              36G   20G   14G  59% /
none                  621M     0  621M   0% /dev/shm
/dev/sdb3             145M   15M  123M  11% /boot
/dev/sdb2              36G   28G  6.5G  81% /home
+ free -m
             total       used       free     shared    buffers     cached
Mem:          1240       1208         31          0        167        735
-/+ buffers/cache:        306        934
Swap:         1906          0       1906
+ echo 'this is the end'
this is the end

As you can see, each line starting with the ‘+’ sign is the command itself, then you have the output of that line.