How to debug Bash scripts

Photo by Sigmund on Unsplash

How to debug Bash scripts

Debugging techniques

Debugging bash scripts can be done using a combination of techniques.

1. Use the built-in set command

The set command allows you to enable debugging for a bash script. It can be used to enable debugging for the entire script or for individual sections of code.

To enable debugging for the entire script, you can add the following line to the beginning of the script: set -x This will print out each line of code as it is executed.

To enable debugging for a specific section of code, you can add a line like this:

set -xv # enable debugging for the following section ... 
# code to debug 
set +xv # disable debugging for this section

2. Use the bash -x command.

The bash -x command is another way to enable debugging for a bash script. It works in the same way as the set command, but you can specify the script to debug directly on the command line:

bash -x myscript.sh

This will print out each line of code as it is executed.

3. Use echo statements.

Adding echo statements to your code is an easy way to debug a bash:

#!/bin/bash

# This is an example of how to debug a bash script with echo statements.

# First, start by adding echo statements to the beginning of the script. This will help you to see what values are being passed to each variable.

echo "Debugging script..."

# Next, add echo statements before each command to see what values are being used.

VAR1="foo"
echo "VAR1 value is: $VAR1"

VAR2="bar"
echo "VAR2 value is: $VAR2"

# Now, add echo statements after each command to see the results.

echo "Running command..."
VAR3=$(echo "$VAR1 $VAR2")
echo "Result of command is: $VAR3"

# Finally, add echo statements at the end of the script to see the final results.

echo "Script finished with result: $VAR3"

4. Use the -v flag to enable verbose mode

sh -v script.sh

5. Use the -n flag to check the syntax of the script without executing it:

sh -n script.sh

6. Use the PS4 variable to display the line numbers in the output:

PS4='$LINENO: '

7. Use the bash debugger (bashdb):

bashdb script.sh

What is set command in Linux

The set command in Linux is used to set and unset values of shell options and positional parameters. It can be used to set shell options, or to display the current values of shell variables. The set command also sets the positional parameters, which are the arguments to the current shell or shell script that are passed from the command line.

Example: To set the positional parameter $1:

$ set one

To unset the positional parameter $1:

$ set --

To enable an option in the shell:

$ set -o nounset

To disable an option in the shell:

$ set +o nounset

Positional parameters are a set of special variables in the bash shell that stores values from the command line. They allow a script to access command line arguments that were passed to the script. For example, the following command passes two arguments to a script:

$ myscript.sh arg1 arg2

The positional parameters in this command are set as follows:

$0: myscript.sh 
$1: arg1 
$2: arg2

Debug scripts with trap command

trap is a bash command that allows you to execute a command or set of commands when a specified signal is sent to the script. This can be useful for debugging bash scripts as it allows you to intercept errors and respond to them appropriately.

#!/bin/bash

# Trap the SIGINT and SIGTERM signals
trap "echo 'Script interrupted!'" SIGINT SIGTERM

# Execute the main script
echo "Running main script..."
sleep 10

# Output a success message
echo "Script completed successfully!"

When this script is run, it will execute the main script and then output a success message when it is completed. However, if the script is interrupted by a SIGINT or SIGTERM signal, the trap command will be triggered and the script will output an error message instead.

To send a SIGINT or SIGTERM signal to a bash script, use the command kill. The syntax is:

kill -s SIGINT/SIGTERM pid_of_script

where pid_of_script is the process ID of the script you want to send the signal to.

References

  1. Debug your shell scripts with bashdb