Wednesday, 5 June 2013

How to run bash script in debug mode

Running the bash script in debug mode help us understand what is going on in the script before getting the output. This also help us find why our program didn't run if there is any issue.

It is just like step by step processing of the bash program.

We can use the below command to run the bash script in debug mode in run time.


bash -x mybashscript.sh


This is my bash script which will print 1 2 and 3 in each line

cat mybashscript.sh
#!/bin/bash

for i in {1,2,3}
do
echo $i
done


This is the output of the script (without debug mode)

./mybashscript.sh
1
2
3


This is the output when the script is run in debug mode

bash -x mybashscript.sh
+ for i in '{1,2,3}'
+ echo 1
1
+ for i in '{1,2,3}'
+ echo 2
2
+ for i in '{1,2,3}'
+ echo 3
3

No comments:

Post a Comment