Bash provides the
set command in order to enable or disable different features. The
set -x command is used to debug bash script where every executed statement is printed to the shell for debugging or troubleshooting purposes. The
set -x command can be directly executed in an interactive bash shell or can be used inside the bash script. Generally, the “set -x” command puts the start of the bash shell after the sh-bang.
Enable Debugging with “set -x”
In this example, we enable debugging by using the “set -x” command. By default, bash does not enable debugging. Every statement is printed to the bash terminal and then the statement results are printed in order to make the check easier. For example first the echo “First step” is printed and then its result is printed. Also, the debugged statements start with
#!/bin/bash set -x echo "First step" echo "Second step"
Disable Debugging with “set +x”
As debugging may create a lot of noise by printing all statements in a bash script, the debugging can be disabled by using
set +x command. In the following example, we will disable debugging after the echo “First step” statement so the echo “Second step” will not be debugged and only the statement output will be displayed.
#!/bin/bash set -x echo "First step" set +x echo "Second step"