2008-08-04

Better shell scripts: set -u

Beginning shell scripts with "set -u" could save you serious time. From bash(1):
Treat unset variables as an error when performing parameter expansion. If expansion is attempted on an unset variable, the shell prints an error message, and, if not interactive, exits with a non-zero status.
Example:
$ cat hello.sh
#!/bin/sh

NAME=$1
echo Hello $NAME
$ sh hello.sh
Hello
vs
$ cat hellou.sh
#!/bin/sh

set -u

NAME=$1
echo Hello $NAME
$ sh hellou.sh
hellou.sh: line 5: $1: unbound variable

No comments: