Linux offers the built-in “set” command to modify the shell arguments and prints the bash script variables. It offers a wide range of supported flags from which the “-x” is utilized to enable and disable the debugging feature in the bash script.
It displays the executed command and its argument in the terminal to debug the bash scripts, exit programs if fail, export variable values, and for troubleshooting purposes.
Considering its importance, this guide explains the working of the “set -x” command in bash script having the following outcomes:
- How Does “set -x” Work in Bash Script?
- Enable Debugging Using “set -x”
- Use “-x” in the Bash Shebang Line
- Disable Debugging Using “set +x”
How Does “set -x” Work in Bash Script?
The bash script offers a set of features in which the most common is the start of the subshell using the “-x” option. It can be used within the bash script and on the terminal.
Here some examples are described to show the working of “set -x” and “set +x” in a bash script.
Example 1: Enable Debugging Using “set -x”
The current working directory contains a bash script named “Program.sh” opened in the “nano” editor having the following content:
$ nano Program.sh
#!/bin/bash set -x d=`date+%y:%m:%d:%H:%M:%S` echo "Today date is $d"
The above code contains the following parameters:
- The first line denotes the “bash shebang” that executes the script in bash shell.
- The second line shows the “set -x” command that enables the debug feature in the bash script.
- The third line specifies the “d” variable that contains the date command value.
- The fourth line displays the output of the “$d” variable alongside the arguments.
The execution of “Program.sh” shows the script content in the terminal including the error in “line 3” due to the usage of the “set -x” command:
The “set -x” command clearly displayed the error in the script and now the user can easily debug it.
Fix Error in the Program.sh Script
The error occurred due to the incorrect syntax of the “date” command typed into the 3rd line of the “Program.sh” script. The updated error-free code of “Program.sh” is:
#!/bin/bash set -x d=`date +%y:%m:%d:%H:%M:%S` echo "Today date is $d"
Execute the “Program.sh” bash script again and check the output:
Now the “set -x” executed the “Program.sh” script line by line successfully as shown in the above output.
Alternative: Use “-x” in the Bash Shebang Line
The user can also pass the “-x” into the bash shebang line of the scripts instead of commands in the following way:
$ nano Program.sh
It provides the same output as the “set -x” command displays as shown in the image:
Disable Debugging Using “set +x”
The “set +x” flag is used to disable the debugging feature of the bash script as it displays all statements in the terminal. The user can use it at any point in the bash script.
For the practical implementation, we used it before the “echo” statement in the “Program.sh” bash script:
$ nano Program.sh
#!/bin/bash d=`date+%y:%m:%d:%H:%M:%S` set +x echo "Today date is $d"
The above “set +x” will just print the “echo” statement output in the terminal:
In a Linux bash script, the “set -x” activates the debugging features to identify the errors clearly. The “set -x” is generally used at different places in the scripts however the user can also utilize it in the “bash shebang” line. Once it is enabled, it can be disabled using the “set +x” command. This guide has provided a deep insight into the purpose, and working of “set -x” in a bash script.