Cybersecurity Updates & Tools

How To Debug Bash Scripts Using bash -x And set Commands

Introduction

Debugging is an important part of Bash scripting. When a script does not work as expected, you need to find where the problem is. Bash debugging helps you understand which commands are running, what values variables contain, and where errors happen.

If you are learning Bash scripting, debugging will save a lot of time. Beginners often face problems like missing permissions, wrong variable names, incorrect paths, syntax errors, and failed commands. Bash provides useful options such as bash -x, set -x, set -e, and set -u to help identify and fix these issues.

What Is Bash Debugging?

Bash debugging means checking your script step by step to find errors. Instead of guessing what went wrong, debugging shows how the script is executed.

For example, if a variable is empty or a command is not running correctly, debugging can help you see the actual command and value used by Bash.

Debug A Script Using bash -x

The easiest way to debug a Bash script is by running it with bash -x.

Create a script:

nano debug-example.sh

Add this code:

#!/bin/bashname="Kali Linux"echo "Welcome to $name"file="/tmp/test.txt"cat "$file"

Now run the script with debugging enabled:

bash -x debug-example.sh

Example output:

+ name='Kali Linux'+ echo 'Welcome to Kali Linux'Welcome to Kali Linux+ file=/tmp/test.txt+ cat /tmp/test.txtcat: /tmp/test.txt: No such file or directory

The + lines show each command before Bash executes it. This makes it easier to find the error.

Use set -x Inside A Script

You can enable debugging inside the script using set -x.

#!/bin/bashset -xusername=$(whoami)current_dir=$(pwd)echo "User: $username"echo "Directory: $current_dir"

Run it normally:

chmod +x debug-script.sh./debug-script.sh

Bash will show each command as it runs.

Turn Debugging Off Using set +x

You can enable debugging for only one part of the script.

#!/bin/bashecho "Script started"set -xfile="/etc/passwd"grep "root" "$file"set +xecho "Script finished"

Here, only the commands between set -x and set +x are shown in debug mode.

Use set -e To Stop On Errors

The set -e command stops the script when a command fails.

#!/bin/bashset -eecho "Checking file..."cat /wrong/file/pathecho "This line will not run"

This helps prevent scripts from continuing after an important error.

Use set -u To Detect Missing Variables

The set -u command stops the script when an undefined variable is used.

#!/bin/bashset -uecho "Username is $username"

If username is not defined, Bash shows an error. This helps catch spelling mistakes in variable names.

Conclusion

Debugging Bash scripts is an important skill for beginners. You can use bash -x to debug a script from the terminal, set -x to debug inside a script, and set +x to stop debug output.

You can also use set -e to stop scripts on errors and set -u to detect undefined variables. These debugging methods are useful for Linux automation, cybersecurity scripts, backup scripts, and system administration tasks