Bash set x что это
Перейти к содержимому

Bash set x что это

  • автор:

Debugging bash scripts

bash-debug

When the bash scripts get big, its often hard to debug while relying only on echo statements trying to trace the error causing statements and functions.

Thankfully there is a setting that can be used to enable debugging and can help you start debugging in a second.

using xtrace -x

At the beginning of a script just add set -x which enables the xtrace mode and then prints out each command it executes before outputting the result.

enabling and disabling -x

The option can also be enabled for specific parts of the script and then disabled by just using set +x . So anything between set -x and set -x will have the trace enabled.

Output:

so the debugging is enabled only around the function which allows us to see what variables are passed like for line 9 it uncovers the variables:

echo «Inside function: var1: $var1, var2: $var2»
to
+ echo ‘Inside function: var1: C, var2: D’ Inside function: var1: C, var2: D

enabling -x debug via command line arguments

If you do not want to add the option to script itself then we can pass the -x as commandline argument to enable debugging for the whole script like:

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.

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.

echo "Print statement1"

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.

MyName =Linux
echo $MyName

MySalary = 7000
echo $MySalary

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.

while [ $y -gt 0 ] ; do

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.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

What does "set -x" do in a bash script?

I am reading a bash script and it start with set -x . I googled around and I did not find any single manual which does describe all the flags in details specially -x .

My apologies if I sound like someone who did not do his research before asking the question here but I sincerely did not find any information on set -x . Any ideas?

1 Answer 1

As help set says:

It works both in interactive and non-interactive shells, so you can try running set -x in an interactive shell to see the effect. Each command that is run is echoed to you first (with + signs in front of them to help you distinguish them from most regular output).

(If you see way more output than you expect an an interactive shell, your shell may be running commands to build your prompt. For example, you’ll see about twenty additional lines if your prompt is set up to show information about the git repository you’re navigated to, even if you’re not actually in a repo now.)

To turn it off run set +x . Somewhat confusingly, with set , — enables a shell option and + disables it.

What does `set -x` do?

set -x enables a mode of the shell where all executed commands are printed to the terminal. In your case it’s clearly used for debugging, which is a typical use case for set -x : printing every command as it is executed may help you to visualize the control flow of the script if it is not functioning as expected.

set +x disables it.

John Zwinck's user avatar

set -x

Prints a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

Example

Above example illustrates the usage of set -x . When it is used, above arithmetic expression has been expanded. We could see how a single line has been evaluated step by step.

  • First step expr has been evaluated.
  • Second step echo has been evaluated.

To know more about set → visit this link

when it comes to your shell script,

Your script might have been printing some additional lines of information when the execution mode selected as DEBUG . Traditionally people used to enable debug mode when a script called with optional argument such as -d

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *