Chapter 1. Why Shell Programming?

A working knowledge of shell scripting is essential to anyone wishing to become reasonably adept at system administration, even if they do not anticipate ever having to actually write a script. Consider that as a Linux machine boots up, it executes the shell scripts in /etc/rc.d to restore the system configuration and set up services. A detailed understanding of these startup scripts is important for analyzing the behavior of a system, and possibly modifying it.

Writing shell scripts is not hard to learn, since the scripts can be built in bite-sized sections and there is only a fairly small set of shell-specific operators and options [1] to learn. The syntax is simple and straightforward, similar to that of invoking and chaining together utilities at the command line, and there are only a few "rules" to learn. Most short scripts work right the first time, and debugging even the longer ones is straightforward.

A shell script is a "quick and dirty" method of prototyping a complex application. Getting even a limited subset of the functionality to work in a shell script, even if slowly, is often a useful first stage in project development. This way, the structure of the application can be tested and played with, and the major pitfalls found before proceeding to the final coding in C, C++, Java, or Perl.

Shell scripting hearkens back to the classical UNIX philosophy of breaking complex projects into simpler subtasks, of chaining together components and utilities. Many consider this a better, or at least more esthetically pleasing approach to problem solving than using one of the new generation of high powered all-in-one languages, such as Perl, which attempt to be all things to all people, but at the cost of forcing you to alter your thinking processes to fit the tool.

When not to use shell scripts

If any of the above applies, consider a more powerful scripting language, perhaps Perl, Tcl, Python, Ruby, or possibly a high-level compiled language such as C, C++, or Java. Even then, prototyping the application as a shell script might still be a useful development step.

We will be using Bash, an acronym for "Bourne-Again Shell" and a pun on Stephen Bourne's now classic Bourne Shell. Bash has become a de facto standard for shell scripting on all flavors of UNIX. Most of the principles dealt with in this book apply equally well to scripting with other shells, such as the Korn Shell, from which Bash derives some of its features, [2] and the C Shell and its variants. (Note that C Shell programming is not recommended due to certain inherent problems, as pointed out in an October, 1993 Usenet post by Tom Christiansen).

What follows is a tutorial on shell scripting. It relies heavily on examples to illustrate various features of the shell. As far as possible, the example scripts have been tested, and some of them may even be useful in real life. The reader should use the actual examples in the source archive (something-or-other.sh), [3] give them execute permission (chmod u+rx scriptname), then run them to see what happens. Should the source archive not be available, then cut-and-paste from the HTML, pdf, or text rendered versions. Be aware that some of the scripts below introduce features before they are explained, and this may require the reader to temporarily skip ahead for enlightenment.

Unless otherwise noted, the book author wrote the example scripts that follow.

Notes

[1]

These are referred to as builtins, features internal to the shell.

[2]

Many of the features of ksh88, and even a few from the updated ksh93 have been merged into Bash.

[3]

By convention, user-written shell scripts that are Bourne shell compliant generally take a name with a .sh extension. System scripts, such as those found in /etc/rc.d, do not follow this guideline.