Debugging shell/bash scripts


Debugging is a important part while writing a scripts.

To debug particular block of code in shell/bash scripts we use set.

With set we can enable and disable debugging.

To enable debugging use set -x before the block of code from where you want to enable debugging.

To disable debugging use set +x after the block of code


for i in {1..6}; do set -x; echo $i; set +x ;done

In above command I am enabling debugging mode for each variable i, so I used set -x before printing the line and set +x after printing the line.

Because of this output of the command will shown with detailed input for every line and output for every line as shown below.


+ echo 1
+ set +x
+ echo 2
+ set +x
+ echo 3
+ set +x
+ echo 4
+ set +x
+ echo 5
+ set +x
+ echo 6
+ set +x



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s