Module-11 How to debug bash script using set   -x option

Table of contents

Introduction

It is very important to understand how to debug a script. While coding new features we might hit some bugs in the code. To debug the issues bash provides some inbuilt features like “set -x” to print the lines of the execution of the code with substitution of the variables along with other echo statement output

Syntax to enable or disable the debug feature in bash scripts

set -x  # enables the debug feature in bash scripts 

set +x  # disables the already enabled debug feature in bash scripts 

Practical example on enabling and disabling the debug feature

In the following example , we are using the debug feature on a simple script to trace the script execution with substitution ( all the lines starting with “+” are printed due to the enabled debug feature “set -x” ). In the same script we disabled the debug feature using (set +x) after a few lines of code. From then on we can observe that the script only prints the echo statement output

#script 
[root@discoveringsystems debug-bash-script]# cat debug-bash
#!/bin/bash

set -x
var_1="red"
echo "The color is $var_1"
set +x
var_2="blue"
echo "The color is $var_2"
#script execution
[root@discoveringsystems debug-bash-script]# ./debug-bash
+ var_1=red
+ echo 'The color is red'
The color is red
+ set +x
The color is blue

How to use the bash script debug feature in  While loop 

In the following script example ,we are using the debug feature to trace the while loop iterations , we can see that each time the while loop starts its iteration we can see the condition is satisfied. All the lines which starts with “+” are printed by the debug feature (set -x )

#script
[root@discoveringsystems debug-bash-script]# cat debug-bash-2
#!/bin/bash

set -x

count=0

while [ $count -lt 2 ]
do
  echo "count is $count"
  ((count++))
done

#script execution
[root@discoveringsystems debug-bash-script]# ./debug-bash-2

+ count=0
+ '[' 0 -lt 2 ']'
+ echo 'count is 0'
count is 0
+ (( count++ ))
+ '[' 1 -lt 2 ']'
+ echo 'count is 1'
count is 1
+ (( count++ ))
+ '[' 2 -lt 2 ']'

How to use the bash debug feature in the scripts involving the if-conditionals 

In the following example, we are using the modified version of the same script  to involve the if-else conditionals to show how the bash debug feature (set -x ) helps in tracing the code with both while-loop and if-conditionals. All the lines printed by the debug feature is prefixed by “+”

#script 
[root@discoveringsystems debug-bash-script]# cat debug-bash-3
#!/bin/bash

set -x

count=0

while [ $count -lt 5 ]
do
  if [ $count -eq 2 ] ; then
    echo "count is $count"
    break
  else
    echo "count is $count"
  fi
  ((count++))
done

#script execution
[root@discoveringsystems debug-bash-script]# ./debug-bash-3
+ count=0
+ '[' 0 -lt 5 ']'
+ '[' 0 -eq 2 ']'
+ echo 'count is 0'
count is 0
+ (( count++ ))
+ '[' 1 -lt 5 ']'
+ '[' 1 -eq 2 ']'
+ echo 'count is 1'
count is 1
+ (( count++ ))
+ '[' 2 -lt 5 ']'
+ '[' 2 -eq 2 ']'
+ echo 'count is 2'
count is 2
+ break

Conclusion

The examples shown in this article helps understanding the debug feature in the bash scripts. Similarly we can use this feature in other scripts involving for-loops , functions , etc 

Leave a Comment

Your email address will not be published. Required fields are marked *