Linux By Karthick Dkk

Debug Bash Script

Bash script step-by-step execution

Karthick Dkk
2 min readDec 18, 2024

--

Photo by RoonZ nl on Unsplash

Hey Mate! Welcome to another blog post!

If you want to see the step-by-step execution of a bash script to understand what it’s doing or to debug it, you can use the -x option when running the script or include it within the script itself. This will display each command as it gets executed, along with any variables that are expanded.

Option 1: Run Script with Debugging (-x)

When you execute the script, add -x to the command:

bash -x script.sh

This will show each command, along with its expanded arguments, as it’s executed.

Option 2: Add Debugging Inside the Script

Add the following line at the top of your bash script to enable debugging for the whole script:

#!/bin/bash
set -x

If you only want debugging for specific parts of the script, you can turn it on and off as needed:

#!/bin/bash
echo "This part runs without debugging"

set -x # Turn on debugging
echo "Debugging starts here"

ls -l

set +x # Turn off debugging
echo "Debugging stopped"

--

--

Karthick Dkk
Karthick Dkk

Written by Karthick Dkk

DevSecOps Engineer passionate about Linux, cloud computing, and automation. Talks about Personal Development and Money.

Responses (1)