| by Arround The Web | No comments

How to Use the Bash “Set –X” Option

The set command of Linux is used to set and unset specific options. These options manage the behavior of a given script and enable the task execution without error. In this article, we use the “x” option with the set command which informs the Bash to display the statements as they are processed. It is immensely useful for debugging when we need to determine which lines were executed and in what sequence. The set -x operation can be employed directly in an open Bash shell or within a Bash script. In the following examples, we use the set command with the “x” option to enable and disable the various functionalities.

Example 1: Program Using the Set -X in a Bash Script

Here, we create the “Bash1.sh” file and provide the script to use the set -x option. We use the “set -x” command to enable the debugging because the Bash script doesn’t enable debugging by default. We first insert the sh-band command in our script. After that, we use the “set” modifier with its “-x” flag to activate debugging. Then, we declare the “echo” command which provides the “My First Statement” statement. Same as this echo statement, we define another “My second statement” statement using the “echo” modifier. Next, we execute the Bash script by opening the terminal to show the results of enabling the debugging from the set -x command.

#!/bin/bash

set -x

echo "My First statement"

echo "My Second statement"

On the terminal, we invoke the “bash” keyword for the compilation of the “Bash1.sh” file. After that, we execute the given file which prints the echo command with the “+” symbol along with the resultant statement from the echo commands.

Example 2: Program Using the Set -X in a Disabled Mode in a Bash Script

In the recent example, we demonstrated the way of enabling the debugging using the “set -x” command. Now, we provide a way of disabling the debugging in the Bash script using the “set” command but with the “x” option in a different format. Here, we construct the script in a “Bash2.sh”. First, we use the “set -x” command which enables the debugging of Bash. Then, we set the “echo” command to print the “print statement1” statement. After this, we deploy the “set +x” command to disable the debugging of the Bash script. The difference in enabling and disabling the debugging is that we use the “x” option with the negative “-” symbol to activate the debugging. The “x” with the positive “+” symbol indicates disabling the Bash debugging.

#!/bin/bash

set -x

echo "Print statement1"

set +x

echo " Print statement2"

The previous Bash script is executed in the terminal using the “bash ./Bash2.sh” command. The result of this script prints the “echo” command with the output of that echo command because the debugging is enabled. Then, the “set +x” command is printed where we disable the debugging. The disabling mode of debugging the Bash simply prints the output of the echo command as seen in the following image:

Example 3: Program Using the Set -X in the Shebang Line Command in a Bash Script

We have another way to enable the debugging in Bash by including the “-x” flag in the shebang line. Here, we create the script inside the “Bash3.sh”. We begin by defining the “#!/bin/bash -x” shebang command where the “-x” option is also declared to enable the debugging of the given Bash script. Then, we create the “MyName” attribute to which we assign a “Linux” value. The attribute is further set with the echo command to print its value. After this, we disable the debugging with the “set +x” statement. We declare the “MySalary” attribute and initialize it with the “7000” value for debugging. The “echo” keyword is called over the “MySalary” attribute to output the value. Next, we define a case again to activate the debugging in the Bash script. For this, we define the “set +x” command. After that, we set the “MyLanguage” attribute again with the “English” value to examine the enabled mode of debugging.

#!/bin/bash -x

MyName=Linux
echo $MyName

set +x

MySalary=7000
echo $MySalary

set -x

MyLanguage=English
echo $MyLanguage

When the “Bash3.sh” is executed in the terminal, it first prints the “MyName=Linux” expression and the echo statement with its resultant value. The “7000” value is the resultant value of the echo expression where the debugging is not activated. Next is also the printed “MyLanguage=English” expression and the echo expression with the “English” output value as the debugging mode is enabled.

Example 4: Program Using the Set -X in a Bash Script for the Conditional Expression

Now, we use the “set -x” command to debug the conditional statement which executes the result as well as prints each step of the output from the Bash script for a better understanding of the script. Here, we generate the Bash script in the “Bash4.sh” file. The first step of the script is to declare the “set -x” command. By using the “set -x” command, we enable the debugging mode of our Bash script. Then, we create the “y” attribute and specify a numerical value of “50”. After that, we deploy the while loop where the condition is given as “$y -gt 0”. The “do and done” statement has the operation to satisfy the while condition. The “echo” command is assigned with a “$y” variable which prints the decrement value each time until the while loop is terminated.

set -x
y=50

while [ $y -gt 0 ]; do

y=$[ $y-10 ]

echo $y
sleep 1
done

The output is displayed with every single step of the script where the conditional values are updated.

Conclusion

The use of the “set -x” command in Bash is a debugging process that troubleshoots the code by recognizing the bugs in the script. The “set -x” comes in help when it’s a challenge for the users to develop an error-free script, especially when it comprises a large number of lines. We provided the Bash scripts using the “set -x” and “set +x” commands for demonstration. Furthermore, we can also use the “x” option on the terminal as “bash -x ./FileName.sh” if we don’t want to use the “set -x” command in the script. We achieve the same result by doing this.

Share Button

Source: linuxhint.com

Leave a Reply