Skip to content

How to Debug a Shell Script?

On this page

Debugging is the process of finding and resolving bugs within a computer program. It provides a huge output at the runtime to analyze each part of the execution. Which helps to identify the root cause of any error in the script. In this tutorial, you will learn how to Debug a shell script on the Linux command line.

We can debug a shell script in two ways. Either add the debugging instruction in the shell script by using “set -xv” or using -xv on command line while executing script.

Adding Debug Instructions in Shell Script

Let’s create a small script to test the debugging process. Create a new file and edit in text editor:

nano checkdebug.sh 

Add the following content to file:

Save changes and close the file.

Next set the execute permissions on file and then execute it.

chmod +x checkdebug.sh 
./checkdebug.sh 

[Sample Output]

cd /var/log/
+ cd /var/log/
for i in "*.log"; do
 du -sh $i
done
+ for i in '"*.log"'
+ du -sh boot.log mysqld.log post111.log post1121.log yum.log
0       boot.log
32K     mysqld.log
0       post111.log
0       post1121.log
4.0K    yum.log

Provide Debug Instructions from Command Line

Using this option we don’t need to add “set -xv” in shell script. Just create a shell script like below

nano checkdebug2.sh 

and Execute like below

sh -xv checkdebug2.sh 

[Sample Output]

#!/bin/bash

cd /var/log/
+ cd /var/log/
for i in "*.log"; do
 du -sh $i
done
+ for i in '"*.log"'
+ du -sh boot.log mysqld.log post111.log post1121.log yum.log
0       boot.log
32K     mysqld.log
0       post111.log
0       post1121.log
4.0K    yum.log

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top