Thursday, April 29, 2010

Debugging a Linux BASH shell script

Debugging shell scripts is really a PITA. Here's a handy trick that really saved me today -
Use -x and -v switches to get more insight into what BASH is doing.
To display commands and their arguments as they are executed -
bash -x scriptName

To display shell input lines as they are read -
bash -v scriptName

You can put the above two commands in the shell script itself, like so -
# turn on debug mode
set -x
set -v

OR change the shabang line -
#!/bin/bash -xv

No comments: