Debugging shell scripts

[problem]

You need to debug a shell script.

[/problem]

[solution]

Most shells (zsh, ksh, bash …) can either run with a minus x (-x) option or by specifying set -x.

If you have an existing shell, that you’d like to run in debug mode – just run it like this: bash -x scriptname. Or ksh -x for korn shell.

[/solution]

[example]

You can also edit the follow and put a minus x after the interpreter line, for example:

#!/bin/ksh -x

or

#!/bin/bash -x

Debugging on the command line can be performed, simply by typing set -x. Like this:


$ set -x
+ set -x
$ date
+ date
Tue Jun 13 18:59:44 WST 2006
$ echo "hello world"
+ echo 'hello world'
hello world
$ set +x
+ set +x
$ date
Tue Jun 13 18:59:57 WST 2006

[/example]

[reference]

[tags]Debug Shell Scripts, Unix Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *