How to Activate Debug Mode in Bash Script? [3 Methods]

When things don’t go as planned, you have to figure out why bash is providing errors. Like other programming languages, bash has many robust tools to debug the script. The most common way to debug is to activate the debug mode in your bash script.

To activate the debug mode in your script, you can choose any of the methods such as writing the set -x command at the beginning of your script, adding the “-x” option after the shebang line or you can call the “-x” option along with the script name while executing your bash file. In this article, I will show all the methods in detail. So let’s start!

What is Debug Mode in Bash Script?

Debug mode in bash script is the state where the script executes with extra information about the execution process. In debug mode, each bash command and its arguments are printed to standard error output before it executes. Upon enabling the debug mode in bash, you see a torrent of information about every command and each command’s arguments flowing through your terminal. This “behind the scenes” view shows you where the error is happening, and helps you find the problem faster than usual debugging techniques.

3 Methods to Activate Debug Mode on Entire Bash Script

To understand the execution of debug mode in a script, the first straightforward approach should be using the -x option along the script name while executing the bash file in your terminal. Other than that, there are two more ways as I stated previously to debug your script. In the following section of this article, you will learn all of them.

1. Adding “-x” Option While Executing the Script

To enable bash debug mode, you can write the -x option before the script name. The basic syntax is bash -x script_name.sh. Here is a sample code for the illustration:

#!/bin/bash

read -p "Enter a number: " number

# Use arithmetic expansion and the modulus operator to check for even or odd
if ((number % 2 == 0)); then
    echo "$number is an even number."
else
    echo "$number is an odd number."
fi
EXPLANATION

This Bash script begins with a shebang line (#!/bin/bash) that designates the Bash shell as the interpreter for executing the script. The read command is then employed to prompt the user to input a number, storing the entered value in the variable number. The script proceeds to check whether the provided number is even or odd using an if statement that utilizes arithmetic expansion and the modulus operator (%). If the result of the modulus operation is zero, the script displays a message saying the number is even using the echo command; otherwise, it echoes a message stating that the number is odd.

Output image of Adding “-x” Option While Executing the ScriptAs you see from the image above, the -x option in the bash -x terminal.sh command allows to have bash debug mode and display each code line before its execution.

Note: If you plan to execute your file using the syntax ./ -x script_name, then this method will not work. This is because the whitespace followed by the ./ syntax will cause an error. The correct way to use this method is by typing bash -x script_name instead.

2. Placing “-x” Option in the Shebang Line

Another method to enable debug mode in a Bash script is by using the shebang line at the beginning of the script and specifying the -x option. You can add the following line at the start of your script:

#!/bin/bash -x 

See a sample code given below to understand its functionality:

#!/bin/bash -x

read -p "Enter a number: " number

# Use arithmetic expansion and the modulus operator to check for even or odd
if ((number % 2 == 0)); then
    echo "$number is an even number."
else
    echo "$number is an odd number."
fi 
EXPLANATION

The script uses Bash to prompt the user for numeric input, checks whether the entered number is even or odd, and outputs a corresponding message.

Placing “-x” Option in the Shebang LineAs the image shown above, the -x option in the shebang line enables the debug mode and shows every line of a script before its execution.

Note: For this method, execution of the file using the syntax bash script_name will not work, and the correct way to do it is by typing ./script_name.

3. Writing “set -x” Command After the Shebang Line

To enable debug mode in a Bash script, you can insert the command “set -x” after the shebang line (#!/bin/bash):

#!/bin/bash

# Use this command to enable the debug mode
set -x

read -p "Enter a number: " number

# Use arithmetic expansion and the modulus operator to check for even or odd
if ((number % 2 == 0)); then
    echo "$number is an even number."
else
    echo "$number is an odd number."
fi
EXPLANATION

The Bash script aims to receive a user-inputted number, determine if it’s even or odd using modulus arithmetic, and communicate the result through an output message. The set -x command is used here to enable the bash debug mode, and provides the standard output of each line written in the script. Thus it allows to look into the code and find the possible error if exists there.

 Writing “set -x” Command After the Shebang LineHere as you can see, adding the set -x command in the script enables it to activate the debug mode. The debug lines are marked with a (+) sign at the beginning.

How to Activate Debug Mode in a Specific Part of Bash Script?

To activate debug mode in a specific part of a Bash script, enclose the desired section within the following commands:

set -x  # Start of the section to be debugged

# Your code here
set +x  # End of the debugged section

Placing “set -x” before the section begins and “set +x” after it concludes enables and disables debug mode selectively. During this debug mode, Bash prints each executed command with its arguments to the standard error output.

Let’s see a detailed example of activating debug mode in a specific part of the bash script:

#!/bin/bash

#enabling debug mode
set -x

read -p "Enter a number: " number

#disabling debug mode
set +x

# Use arithmetic expansion and the modulus operator to check for even or odd
if ((number % 2 == 0)); then
    echo "$number is an even number."
else
    echo "$number is an odd number."
fi
EXPLANATION

This Bash script is designed to gather a numerical input from the user, assess whether the entered number is even or odd using modulus arithmetic, and then convey the result through an output message. set -x command is used to enable and set +x to disable the debug mode.

Activate Debug Mode in a Specific Part of Bash ScriptHere, you can see from the image given above, that after executing the set -x command, the terminal shows each bash line of the script before its execution with a “+” sign. After executing the set +x command, the terminal no longer shows the bash mode and only displays the output of subsequent scripts.

Conclusion

To sum up, enabling debug mode in a bash script using the “set -x” command is a useful tool to troubleshoot and understand the execution of the script. This mode prints all the commands and their arguments to standard error output, providing a detailed description of the script’s execution. By enclosing certain sections with “ set -x” and “set +x”, you can selectively turn on and off debug mode, concentrating on the areas that need more attention. For further assistance or queries, please do common below. Our technical team is one click away to answer your questions. Thank You!

People Also Ask

Can we debug shell script?

Yes, shell scripts can be debugged. To debug a shell script, you can use tools like bash -v to enable debugging output or insert set -v in the script to trace each command before it’s executed. Additionally, you can use echo statements to print variable values and intermediate results.

How do I enable debug mode in Linux?

To enable debug mode in Linux, you can use the -x option with the Bash shell or include the command set -x in your script. This activates debugging, providing a detailed trace of each command executed, helping you identify and resolve issues in your script.

How do I enable debug mode in SSH?

To enable debug mode in SSH (Secure Shell), you can use the -v (verbose) option, which increases the verbosity level of the SSH client. You can add one or more -v options to increase the verbosity further.

Here’s an example:

ssh -v user@hostname

This command initiates an SSH connection to the specified hostname with the username user and enables verbose mode. If you need even more details, you can add additional -v options, like this:

ssh -vv user@hostname

Each -v increases the verbosity level, and you can use up to three for more detailed debugging information. This can help diagnose connection issues, authentication problems, or other complications when using SSH.

How do I stop debug in Linux?

To stop a Bash script running in debug mode (activated with set -x) you can use the command set +x in your script. This will disable the debug mode for the remaining part of the script.

Though it is a generalized format to do so, but the exact method may vary depending on the tool or process you’re working with.

Related Articles


<< Go Back to Bash Debugging | Bash Error Handling and Debugging | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment