1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – Debugging

Bash – Debugging

Debuging A Bash Script

Bash scripting provides an option to debug your script at runtime. You using “set -xv” command inside shell script or using -xv on command line while executing script.

Syntax:

$ bash -xv script.sh

Example – Enable Debug in Script

You can enable debug mode by adding set -xv option inside a shell script. This is useful to enable debugging for some part of the script.

#!/bin/bash

set -xv   # this line will enable debug

cd /var/log/
for i in "*.log"; do
 du -sh $i
done

Now execute the script, You will found result like

$ ./script.sh

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

Example – Enable Debugging Run Time

You can enable debug at script run time using -xv option. For example

$ bash -xv script.sh