This chapter has a bunch of tricks and techniques for programming
with the Bourne shell.
Some of them are documented but hard to find; others aren't documented
Here is a summary of this chapter's articles:
The first group of articles is about
making a file directly executable
on the first line.
On many versions of UNIX (see article
can start with a first line like this:
The kernel will start the program named in that line and give it the
file to read.
Chris Torek's Usenet classic, article
explains that your "shell scripts" may not need a shell at all.
will give you a few grins as it shows unusual examples of
has experiments to help you understand what
If your UNIX doesn't have
, the trick in article
will let you be sure your scripts run with the Bourne shell.
Scripts using an interpreter that isn't a shell are in articles
The next five articles are about
processes and commands
replaces the shell with another process; it can also be used to change
input/output redirection (see below).
command can control how signals are passed to child processes;
(colon) operator evaluates its arguments and returns a
zero status - article
explains why you should care.
UNIX keeps a file on-disk once it's been opened; as article
explains, this has its ups and downs.
is useful for all kinds of operations with lists of numbers and characters.
Next are techniques for handling variables and parameters.
Parameter substitution, explained in article
is a compact way to test, set, and give default values for variables.
You can use the
and UNIX links to make the same script
have multiple names and do multiple things; see article
shows the easy way to get the last command-line argument.
has an easy way to remove all the command-line arguments.
Four articles cover
loop usually reads a list of single arguments into a single
shows how to make the
loop read from standard input.
has techniques for making a
loop set more than one variable.
commands can be used to split pathnames
with a loop; see article
loop can have more than one command line at the start;
Next is an assortment of articles about input/output.
introduces open files and file descriptors - there's more to know about
standard input/output/error than you might have realized!
has a look at file descriptor handling in the Bourne shell, swapping
standard output and standard error.
The shell can redirect the I/O from all commands in a loop at once;
explains one use for this technique
explains good and bad points of doing this.
The shell can read commands directly from a shell script file.
points out, a shell can also read commands from its standard input, but
that can cause some problems.
shows one place scripts from
writing a script that creates
another script as it goes.
Next are two articles about miscellaneous I/O.
One gotcha with the here-document operator (for redirecting input from a
script file) is that the terminators are different in the Bourne and C shells;
shows how to turn off echoing while your script reads a "secret"
answer such as a password.
show uses for the versatile
command substitution (
is similar to
doesn't need a RETURN after the answer;
also can prompt and do basic tests on the answer.
shows a trick for making one
test two things at once.
has a trick for simulating arrays in the Bourne Shell.
to get a control character in a script
without typing the literal character into the file. Finally, article
has a simple technique for getting exclusive access to a file or other