Bash set command is used for many purposes in bash and it has many options to enable or disable different types of features of the shell environment. The -x option is one of the options of the set command that is used to enable the debugging feature of the bash script for troubleshooting. The `set -x` command can be used from the terminal or inside any bash script. Different purposes for using the `set -x` command have been described in this tutorial.
Enable Debugging
If the script does not show the correct output or unexpected output then this problem can be solved by using debugging. The debugging feature can be enabled in different ways in the bash script which are mentioned below.
The debugging feature can be enabled by using the `set -x` command in the terminal or inside the bash script by using the `set –x` command. The use of the `set -x` command has been shown in the next part of the tutorial.
The debugging feature can also be enabled by using the -x option at the time of executing the bash file like the following command:
The debugging feature can also be enabled by using the following line at the beginning of the script:
Disable Debugging
It requires disabling the debugging feature of the bash script after solving the problem. The `set +x` command is used to disable the debugging feature.
Different Examples of the `set -x` Command
Two different uses of the `set -x` option have been shown by using examples.
Example-1: Enable and Disable the Debugging Feature
Create a bash file by using the following script that will take input from the user before and after using the `set -x` and `set +x` commands. The first name value will be taken and printed before using the `set -x` command. The last name value will be taken and printed after using the `set -x` command. Next, the values of the first name and the last name will be printed after using the `set +x` command.
#Take input and print
read -p "Enter your first name: " firstname
echo "Your first name is $firstname"
#Enable the set -x command
set -x
#Take another input and print
read -p "Enter your last name: " lastname
echo "Your last name is $lastname"
#Disable the set x command
set +x
#Print the variables
echo "Your fullname is $firstname $lastname"
Output:
The following output will be printed after executing the above script. According to the output, each statement of the script has been printed after executing the `set -x` command.
Example-2: Debugging Script Using `set -x` Command
Create a bash file with the following script that will take a file name from the user for reading by enabling the debugging feature. The `set -x` command has been used at the beginning of the script to enable the debugging feature. Next, a filename will be taken from the user. If no file name is provided by the user then a message will be printed. If the filename taken from the user does not exist or is not a file then another error message will be displayed. If the valid filename is taken from the user then the content of the file will be printed.
#Enable the debugging
set -x
#Take the filename from the user
echo -n "Enter a filename:"
read fn
#Check whether the filename is empty or not
if [ "$fn" == "" ];
then
echo "Filename is missing."
#Check whether the file exists or not
elif [ ! -f "$fn" ];
then
#Print error message
echo "$fn does not exist."
else
#Print the file content
cat "$fn"
fi
Output:
The following output will appear after executing the above script without providing any filename. The output shows that the empty value has been taken as the filename. The `if` condition for checking an empty filename has been returned true for the empty input. So, the `echo` statement for printing error message has been printed for debugging option and the error message, “Filename is missing” has been printed.
The following output will appear after executing the above script with a filename. The output shows that the `if` condition for checking empty filename has been returned false and the `if` condition for checking non-existing file has been returned false also. The input filename, testfile.txt exists in the current location. So, the `cat` command for reading the testfile.txt file has been executed and the content of the file has been printed.
The following output will appear after executing the above script with a filename. The output shows that the `if` condition for checking an empty file name has been returned false and the `if` condition for checking a non-existing file has been returned true because the file1.txt does not exist in the current location. So, the `cat` command for reading the file has not been executed and the error message, “file1.txt does not exist.” has been printed.
Conclusion
The debugging task is a very important part of any programming language to find out the error of the code easily. The debugging task is done in different ways for different programming languages. Multiple ways of enabling debugging feature for the bash script and the uses of the `set -x` command for debugging the bash script have been described in this tutorial.