Departments & Programs


Shell programming

Last revision August 6, 2004

Table of Contents:
  1. Basic types of statements in a shell script
  2. How do you execute a shell script?
  3. Examples of simple scripts
  4. Working with script variables, including command-line arguments
  5. Command substitution
  6. Expressions involving variables
  7. Other forms of input to shell variables or commands in a script
  8. Flow-of-control statements

This discussion applies to scripts written for the C-shell (csh). Bourne shell (sh) scripts are similar, although the exact syntax differs.

A shell script is a file containing a set of commands to be executed (run) by the shell in sequence as it reads the file.

In its simplest form, a shell script is simply a way to save a set of commands that are often executed, such as the initialization commands for your login session that are stored in the .login script file, so you don't have to re-type the set everytime you want to run it. Instead, you simply run the script that contains all the commands.

Strictly speaking, everything that you can put into a shell script can also be executed interactively by typing on the command line, although the looping constructs can be cumbersome. This gives you the chance to test out the syntax of various shell constructs.

The shell provides tools to make shell scripts more powerful, even full-fledged programs. Shell programming is organized around the concepts of "substitution" and "flow-of-control".

  • Substitution is used to manipulate values within the script. It involves using the values of variables as part of a command; and taking the output of a command to be the new value of a variable.
  • Flow-of-control refers to common programming constructs such as loops and if-then-else statements that are used to control which statements in the script are executed in which order, often depending upon the value of a variable; or to cause repetitive execution of a set of commands with different variable or input values.

Comments or Questions?