9
Aug

Debug Scripts with ‘set -x’

A valuable tool when developing and debugging scripts is set. In particular, the -x or -xtrace options of set will “print a trace of simple commands and their arguments after they are expanded and before they are executed”. Here’s an example of using set -x:

Script

$ cat setx.sh 
#!/bin/sh

echo "Hello There!"
echo "Here's the date..."
set -x
date
echo "Also, a calendar..."
cal
echo "Bye!"
$ 

Execution

$ ./setx.sh 
Hello There!
Here's the date...
+ date
Sun Aug  8 21:33:03 MST 2010
+ echo 'Also, a calendar...'
Also, a calendar...
+ cal
    August 2010
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

+ echo 'Bye!'
Bye!
$ 

Be sure to check out the man page for even more useful options of set.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 0 Comment So Far

Share your thoughts, leave a comment!