Open In App

How to Enable Shell Script Debugging Mode in Linux?

Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on request) in nature like Ubuntu, CentOS, etc. 

Bash is a very popular shell scripting language that is executed in the back-end when you open the terminal in Linux. DevOps (Operations Team responsible for deployment more often) use Bash/Shell scripting (series of commands written in a file to execute together in an ordered fashion) to automate usual/minimalist/trivial tasks for example running checking of time stamps on which file is generated/updated, checking for a number of files, etc.



Debugging is terminology that means the process of identifying errors and possibly resolving them in computer hardware or software. Just like all the other programming/coding languages debugging is crucial in bash/shell scripting in order to understand the flow of the program and resolve any errors if they occur. 

In this article let us discuss two ways of debugging a shell script, to help strengthen the understanding I will try to take 1 simple shell script (Hello World and List) and will build on it with an example of a conditional shell script (odd/even).



1. Using bash options (-x, -n, -v): The following are various options explained in detail:

#! /bin/bash

# print Hello World
echo 'Hello World'

# Listing all components on root
ls /

Output:

Figure 1: Tracing HelloWorld.sh Output using -x option of bash.

In the above figure we see code traced (printed) before it is executed, then its relevant output, and further the code for listing (ls) is traced followed by its execution. Let’s build further with the following example (OddEven.sh) which prompts the user for input and tells whether the entered number is Odd/Even Number.

#! /bin/bash

# Take input from the user
echo -n "Enter a number"
read n

# Calculate remainder using mod (%) operator
remainder=$(( $n % 2 ))

# Odd or Even Check
if [ $remainder -eq 0 ]
then
 echo "You have entered $n -- which is an Even number"
else
 echo "You have entered $n -- which is an Odd number"
fi

Output:

Figure 2: Tracing OddEven.sh Output using -x option of bash.

Similar to the prior example each line of code is traced before it is executed.

#! /bin/bash

# Take input from the user
echo -n "Enter a number"
read n

# Calculate remainder using mod (%) operator
remainder=$(( $n % 2 ))

# Odd or Even Check
if [ $remainder -eq 0 ]
then
 echo "You have entered $n -- which is an Even number'
else
 echo "You have entered $n -- which is an Odd number"
fi

Output:

Figure 3: Checking Syntax-Errors in OddEven.sh using -n option of bash.

When the script is executed with -n option output shows the exact line number location of the line of code where it got a syntax error. Then when the error is fixed and run it again with the same option the following output is shown-

Figure 4: After correcting Syntax-Errors in OddEven.sh using -n option of bash.

This shows that the script is syntax-errors-free.

Figure 5: View Comments and Code while they are executing in OddEven.sh

In the above code all comments and code are visible (like the entire “if block” and not only the lines which are executed).

One slight drawback with this is there is a need to perform debug checks on the entire script, if the script is huge it is quite tough/tedious to find useful insights from the output of -x option. So let us have a look at debugging only some specific part of a script in the Next way of Debugging.

2. Using set -x inside script for the specific script.

Let’s assume we want to debug/trace only a certain logical part of OddEven.sh Script and not the whole script. We can set -x flag before the code we want to debug and then set it again back to +x where tracing of code needs to be disabled. The following example code and screenshot help us depict this scenario.

#! /bin/bash

# Take input from the user
echo -n "Enter a number"
read n

# Enabling trace
set -x

# Calculate remainder using mod (%) operator
remainder=$(( $n % 2 ))

# Disabling trace
set +x

# Odd or Even Check
if [ $remainder -eq 0 ]
then
 echo "You have entered $n -- which is an Even number"
else
 echo "You have entered $n -- which is an Odd number"
fi

Output:

Figure 6: Debugging/Tracing on certain lines of code in a Shell Script.

Here in this example only the logical bit of OddEven.sh Script is being debugged. Only those outputs are traced in terminal output where the set -x option is specified.

Article Tags :