By now you are familiar with using the shell interactively to enter commands. In addition to being a command interpreter, however, the shell is a full-fledged programming language. A program written in the shell language (or, as some users would say, written “in shell”) is often called a shell script. A shell script is just a sequence of commands that have been saved in a file. In fact, any commands you might enter at the command line can be made into a script, and any script that you might write can also be executed just by entering the commands in the file at the command line. This makes basic shell programming very easy to learn, even if you have never programmed before. The shell configuration files (such as your .profile or .bashrc) are examples of shell scripts.
This chapter shows you how to program in shell, including how to:
Write and execute simple shell scripts
Include UNIX System commands in shell programs
Use shell features like variables and I/O redirection in your scripts
Pass arguments and parameters to shell scripts
Make logical tests and execute commands based on their outcome
Use branching and looping operators
Use arithmetic expressions in shell programs
This chapter covers Bourne shell (sh) style scripting only This includes the ksh and bash shell languages. It will not cover scripting in csh or tcsh. These shells are much less commonly used for scripting than the Bourne-compatible shells, and in fact they lack some features that are important for scripting. If you use csh or tcsh as your interactive shell, you can still use sh to run the scripts described in this chapter. See the section “Other Ways to Execute Scripts” for details.