What does `set -x` do?

LinuxBashUnixScripting

Linux Problem Overview


I have a shell script with the following line in it:

[ "$DEBUG" == 'true' ] && set -x

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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.

[source]

Example
set -x
echo `expr 10 + 20 `
+ expr 10 + 20
+ echo 30
30

set +x
echo `expr 10 + 20 `
30

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,

[ "$DEBUG" == 'true' ] && set -x

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

Solution 3 - Linux

> -u: disabled by default. When activated, an error message is displayed when using an unconfigured variable. > > -v: inactive by default. After activation, the original content of the information will be displayed (without variable resolution) before the > information is output. > > -x: inactive by default. If activated, the command content will be displayed before the command is run (after variable resolution, there > is a ++ symbol).

Compare the following differences:

/ # set -v && echo $HOME
/root
/ # set +v && echo $HOME
set +v && echo $HOME
/root

/ # set -x && echo $HOME
+ echo /root
/root
/ # set +x && echo $HOME
+ set +x
/root

/ # set -u && echo $NOSET
/bin/sh: NOSET: parameter not set
/ # set +u && echo $NOSET

Solution 4 - Linux

Instead of giving set -x and set +x , we can run script using -x Example : ksh -x script_name.ksh Please correct me or enhance my knowledge if I'm wrong

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleView Question on Stackoverflow
Solution 1 - LinuxJohn ZwinckView Answer on Stackoverflow
Solution 2 - LinuxRajuView Answer on Stackoverflow
Solution 3 - LinuxlupguoView Answer on Stackoverflow
Solution 4 - LinuxFaisalView Answer on Stackoverflow