Shell tips 1: Options

December 4, 2010

The shell has many options: two of them are particularly useful for programme development.
Although they can be set on invocation like other options, it is generally more useful to use the “set” command which
can be placed within a script.
In most cases these lines should be added at the top of the file
To make your life easier, add the line

set -u

to the beginning of you scripts. This is the rough equivalent of
PERL’s “use strict”. It will give you an error message if you
attempt to use an undefined variable. Undefined variables are
a common headache and cause all sorts of weird behaviour. For instance

if [ -f $MyFile ]

will evaluate to

if [ -f ]

if MyFile is unassigned, which is a *syntax error*.
Another example:

cat $MyFile

will *hang on inpu* if myfile is undefined, since that is what


by itself does. And you don’t get any error message, the script just stops!

If you find you have mysterious hanging problems or othe puzzling
behaviour another global option you can use is

set -x

which basically prints each line (on stderr) as it is executed.
We do not recommend you leave
this switched on all the time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: