Shell Scripts

As noted earlier, Unix shells are all fully-featured programming languages as well as simple ways of invoking programs. A file containing a number of shell commands is called a shell script.

Simple shell scripts are simply a sequence of simple commands (such as are many .profile files). However, a full set of iterative and conditional constructs are also available. The manual pages for each shell will give a complete description of the features available; though they do tend to be fairly cryptic and often a book from the book shop is a more useful approach to learning shell scripting.

The very big advantage of shell scripts is that they provide a simple way of automating a number of otherwise manual tasks. If you need to type a sequence of commands in more than twice, you probably want to write shell script. Remember, we programmers are all tool makers; we like making things easier for ourselves.

For a shell script to be usable as a program:

  • It must live in a directory in your $PATH (see What is a PATH, section 3.4, page [*]).
  • It must have an appropriate execute permission set (see Permissions, section 2.2.16, page [*]).
  • The first line must consist of the string: `#!/bin/sh' (or pathname of your shell).
  • It must not be called test (strange things will break).

Finally, it is traditional, though not at all compulsory, for shell scripts to be written in Bourne (or compatible) shell; this makes portability easier.


But keep an open mind. For a number of problems, other tools such as perl, tcl/tk or awk might be a better solution. It is worth being familiar with a number of tools so that you can use the right tool for each task.

Loc Van Huynh 2007-03-15
Please complete our new website survey