Bash tips - Debugging bash scripts

Written by anil on June 23rd, 2007 in Bash Scripting, Linux.

Developing BASH scripts are easy when comparing with other scripting languages .  .  .  :)  But many times when error occurs, we wish a debugging mode for BASH. The truth is BASH provides a debugging mode that most of the people don’t know (use man . . . man!!).

Running a bash script in debugging mode . . .

bash -x scrtip-name.sh

Sometimes we just need debug only some areas of the script. For that use,

set -x
. . . .
. . . .
set +x

See a sample implementation and output . . .

#!/bin/bash
TERMS=”linux security firwall”

set -x
for i in $TERMS; do
echo $i
done
set +x

For running the script, just give the script name provided the script is executable.

./test-debug.sh

See the output in debugging mode,

-sh-2.05b$./test-debug.sh
+ echo linux
linux
+ echo security
security
+ echo firwall
firwall
+ set +x

Thanks

One Response to “Bash tips - Debugging bash scripts”

  1. Bash tips - a small update to debugging bash scripts Says:

    […] Here is a small update to,  Bash tips - Debugging bash scripts  […]

Leave a Reply



Site Navigation