How to Debug a Bash Shell Script in Linux

In most of the programming languages, debugger tool is available for debugging. A debugger is a tool that can run a program or script that enables you to examine the internals of the script or program as it runs.

In this post, we will learn how to debug a bash shell script line by line in linux. In the shell scripting we do not have any debugger tool but with the help of bash command line options like -n, -v and -x we can do the debugging.

Checking Shell Script for Syntax Error

When we run the script using -n option in bash command then it will not execute the script but it will read the script and validate the syntax and will report errors if any.

In other words, we can say -n option, shot for noexec (as in no execution), tells the shell to not run the commands. Instead, the shell just checks for syntax errors.

Let’s create a script with following content,

$ vi debug_quotes.sh
#!/bin/bash
echo "USER=$USER"
echo "Today's Date: $(date)
echo "SHELL=$SHELL"

save and close the file.

Now try run the script with -n option,

$ bash -n debug_quotes.sh
debug_quotes.sh: line 4: unexpected EOF while looking for matching `"'
debug_quotes.sh: line 5: syntax error: unexpected end of file
$

Output  above shows that there is syntax error, double quotes ‘”‘ is missing. To fix this, put double quotes at end of line which shows today’s date.

Debug-Quotes-Shell-Script-Linux

Running Shell Script in Verbose Mode

The -v option in bash command tells the shell script to run in verbose mode. In practice, this means that shell will echo each command prior to execute the command. This is very useful in that it can often help to find the errors.

Let’s us a shell script with the name “listusers.sh” with following content,

$ vi listusers.sh
#!/bin/bash
cut -d : -f1,5,7 /etc/passwd | grep -v sbin | grep sh | sort > /tmp/users.txt
awk -F':' ' { printf ( "%-12s %-40s\n", $1, $2 ) } ' /tmp/users.txt

#Clean up the temporary file.
/bin/rm -f /tmp/users.txt

Execute the script with -v option,

$ bash -v listusers.sh

output,

list-users-script-output-linux

In output above , script output gets mixed with commands of the scripts. But however , with -v option , at least you get a better view of what the shell is doing as it runs your script.

Running Script with -n and -v option

We can combine the command line options ( -n & -v ). This makes a good combination because we can check the syntax of a script while viewing the script output.

Let us consider a previously used script “debug_quotes.sh”

$ bash -nv debug_quotes.sh
#!/bin/bash
echo "USER=$USER"
echo "Today's Date: $(date)
echo "SHELL=$SHELL"
debug_quotes.sh: line 4: unexpected EOF while looking for matching `"'
debug_quotes.sh: line 5: syntax error: unexpected end of file
$

Debug Shell Script Line by Line

The -x option, short for xtrace or execution trace, tells the shell to echo each command after performing the substitution steps. Thus , we can see the values of variables and commands. Often, this option alone will help to diagnose a problem.

In most cases, the -x option provides the most useful information about a script, but it can lead to a lot of output. The following example show this option in action.

$ bash -x listusers.sh
+ sort
+ grep sh
+ grep -v sbin
+ cut -d : -f1,5,7 /etc/passwd
+ awk -F: ' { printf ( "%-12s %-40s\n", $1, $2 ) } ' /tmp/users.txt
linuxtechi linuxtechi,,,
root root
+ /bin/rm -f /tmp/users.txt
$

As we can see that shell has inserted a + sign in front of each command.

Debug Shell Script with ShellCheck

Shellcheck is a free tool that provides warnings and suggestions for our bash or shell scripts. It points out the syntax issues and  semantic problems of our shell script.

To use shellcheck, first install it using following command,

$ sudo apt install shellcheck     // Ubuntu & Debian

For RHEL / Fedora / CentOS

$ sudo dnf install epel-release -y
$ sudo dnf install ShellCheck

Let’s use the above debug script and run shellcheck on it,

$ shellcheck debug_quotes.sh

ShellCheck-Script-Output

To suppress the warning message use -e flag as shown below,

$ shellcheck -e SC1078 debug_quotes.sh

Exclude-Warning-Shellscheck-Script

Output above confirms that there is a syntax error on line 3, if we put double quotes at end of line 3 then syntax error will be fixed.

That’s all from this post. I you have found it informative and useful. Please do not hesitate to post your queries and feedback in below comments section.

Also Read: How to define and use functions in Linux Shell Script

Leave a Comment