Debugging Shell Scripts

If you write shell scripts of any complexity at all, sooner or later bugs are going to creep in. Although there is no full-fledged debugger for the shell, it does provide some primitive debugging capabilities in the form of being able to trace each action that is performed. You turn on tracing by adding -xv to the end of the #!/bin/sh line in your script, so it looks like this:

#!/bin/sh -xv

The –x switch means for the shell to print every command, preceded by a plus sign (+), to STDERR. The –v switch puts the shell in verbose mode. This has the effect of causing the script to write out everything it does to the error filehandle so that you can debug more easily.

This works best if you pipe the output to more or less

Get FreeBSD6 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.