Debugging Linux Bash Scripts – POFTUT

Debugging Linux Bash Scripts


[rps-include post=6835]

While writing bash scripts we generally expects some results and provide these results to other scripts. But things goes not well as we expect. So we need to see under the hood and what is happening while script is running. Debugging is inspecting bash scripts to see what is wrong with script or show detailed information about script run.

Debugging Whole Script

To make whole script debug mode use -x parameter when calling the script

$ bash -x run.sh 
+ hostname -I
10.101.61.12 192.168.122.1 172.17.0.1

Our script is simple just issue host name -I command. As we can see that issued command is echoed to the console with +sign.

Debugging Some Part of Script

Debugging is useful even we have no problem with the script. It can give information about execution of the script. For long and big script files it may become hard to look debug. Some part of the bash script can be made debug-able like below.

#!/bin/bash

set -x
echo "Debug is on"

set +x
echo "Debug is off"

This is our script where we set on debug with set -x and then set off with set +x.

$ ./run.sh 
+ echo 'Debug is on'
Debug is on
+ set +x
Debug is off

After activating debug we can see the script command but after disabling debugging we can only see script output and cannot see script commands.

[rps-include post=6835]

LEARN MORE  Introduction To Bash Scripting

1 thought on “Debugging Linux Bash Scripts”

  1. It’s a brilliant tutorial. Easy to understand and it’s Newbie-Friendly. I read this tutorial for a whole night and practiced for BASH again and again. I feel I can really do some jobs with BASH now. Thanks for İsmail Baydan, very nice tutorial.

    Reply

Leave a Comment