Portable Shell Programming
An Extensive Collection of Bourne Shell Examples

Table of Contents (PDF file)

Portable Shell Programming book coverMost engineers find that the easiest way to learn shell programming is to learn from the examples of others. Similarly, the easiest way to write a shell script is to use an existing shell script as a template. This stems from the fact that there are innumerable commands and techniques that can be used to solve any particular problem. Seeing an example helps you sift through the chaff to get to a solution quickly. Oftentimes shell programming is used to solve a short term problem, and, therefore, it is important to get something working quickly; the task might not be worth doing if it requires a significant amount of learning or trial and error to come up with a solution.

Seeing examples also helps you to remember the peculiarities of the shell syntax and the idiosyncrasies of the UNIX commands. Since writing shell scripts is not an everyday task for most engineers, it is easy to forget those infrequently used details. It is usually easier to modify a shell script that contains many of the ideas that you need than it is to develop a new shell script from scratch.

Shell programming is too useful to avoid simply because you find the syntax difficult to remember or the number of UNIX commands overwhelming. Besides executing simple commands such as echo, date, cp, and so forth, the shell defines a command language that allows you to combine commands into complex shell scripts. The shell supports variables, control flow statements (such as if, while, and for), functions, parameter passing, and even signal handling. Coupling the capabilities of the shell with the wide variety of commands available on UNIX, very powerful shell scripts can be written in just a few lines. In addition, since the shell is an interpretive language, there is no compiling or linking to worry about. You can create simple and powerful shell scripts in just a few minutes.

When I first began to write shell scripts, I found it difficult to find useful examples of shell scripts, and when I did find them, they were often difficult to understand. To this end, I believe it is useful to have a reference of relevant, well documented examples, which is what I have tried to provide in this book. I have also tried to provide enough variety in the examples and their explanations that you will become familiar with many of the common practices used in writing shell scripts, and, therefore, it will be easier for you to understand the code in other shell scripts that you find.

This is not a book on how to use UNIX as most books on shell programming are. This book is intended for people who are already familiar with UNIX, and for one reason or another, have decided to write a shell script. This book is intended for people who already know what an if statement is, but they do not know, or cannot remember, the syntax for an if statement in the shell. If they could just see an example, they would be off and programming. The examples in this book cover everything from simple shell statements to complex shell scripts, and unlike most books on shell programming, this book contains examples that you are likely to use.

See the reviews or order this book from Amazon.com.

Examples from the Book

You can click the links below to get any of the examples from the book. The tar file contains all of the examples.

Shell Commands

Shell Functions