Debugging frequently takes longer than writing code. A feature every programming language should implement is to produce trace information when something unexpected happens. Debugging information can be read to understand what caused the program to behave in an unexpected fashion. Bash provides debugging options every developer should know. This recipe shows how to use these options.
Debugging the script
How to do it...
We can either use Bash's inbuilt debugging tools or write our scripts in such a manner that they become easy to debug; here's how:
- Add the -x option to enable debug tracing of a shell script.
$ bash -x script.sh
Running the script with the -x flag will print each source line with the current status.
- Debug only portions of the script using set -x and set +x. Consider this example:
#!/bin/bash #Filename: debug.sh for i in {1..6}; do set -x echo $i set +x done echo "Script executed"
In the preceding script, the debug information for echo $i will only be printed, as debugging is restricted to that section using -x and +x.
The script uses the {start..end} construct to iterate from a start to end value, instead of the seq command used in the previous example. This construct is slightly faster than invoking the seq command.
- The aforementioned debugging methods are provided by Bash built-ins. They produce debugging information in a fixed format. In many cases, we need debugging information in our own format. We can define a _DEBUG environment variable to enable and disable debugging and generate messages in our own debugging style.
Look at the following example code:
#!/bin/bash function DEBUG() { [ "$_DEBUG" == "on" ] && $@ || : } for i in {1..10} do DEBUG echo "I is $i" done
Run the preceding script with debugging set to "on":
$ _DEBUG=on ./script.sh
We prefix DEBUG before every statement where debug information is to be printed. If _DEBUG=on is not passed to the script, debug information will not be printed. In Bash, the command : tells the shell to do nothing.
How it works...
The -x flag outputs every line of script as it is executed. However, we may require only some portions of the source lines to be observed. Bash uses a set builtin to enable and disable debug printing within the script:
- set -x: This displays arguments and commands upon their execution
- set +x: This disables debugging
- set -v: This displays input when they are read
- set +v: This disables printing input
There's more...
We can also use other convenient ways to debug scripts. We can make use of shebang in a trickier way to debug scripts.
Shebang hack
The shebang can be changed from #!/bin/bash to #!/bin/bash -xv to enable debugging without any additional flags (-xv flags themselves).
It can be hard to track execution flow in the default output when each line is preceded by +. Set the PS4 environment variable to '$LINENO:' to display actual line numbers:
PS4='$LINENO: '
The debugging output may be long. When using -x or set -x, the debugging output is sent to stderr. It can be redirected to a file with the following command:
sh -x testScript.sh 2> debugout.txt
Bash 4.0 and later support using a numbered stream for debugging output:
exec 6> /tmp/debugout.txt BASH_XTRACEFD=6