Unix Power ToolsUnix Power ToolsSearch this book

27.3. What's a Shell, Anyway?

A shell is a program that interprets your command lines and runs other programs. Another name for the shell is "command interpreter." This article covers the two major Unix shell families, including discussion about how shells run, search for programs, and read shell script files.

27.3.1. How Shells Run Other Programs

For each command it runs, a shell performs a series of steps. First, if the shell is reading commands from a terminal (interactively), it prints a prompt (such as % or $) and waits for you to type something. Next, the shell reads the command line (like cat -v afile bfile > cfile), interprets it (Section 27.1), and runs the result. When the command finishes running (unless the command is in the background (Section 23.2)), the shell is ready to read another command line.

27.3.2. Interactive Use Versus Shell Scripts

A shell can read command lines from a terminal or it can read them from a file. When you put command lines into a file, that file is called a shell script (Section 35.1) or shell program. The shell handles the shell script just as it handles the commands you type from a terminal (though the shell uses its non-interactive mode (Section 3.4), which means, basically, that it doesn't print the % or $ prompts, among other things). With this information, you already know how to write simple shell scripts -- just put commands in a file and feed them to the shell!

In addition, though, there are a number of programming constructs that make it possible to write shell programs that are much more powerful than just a list of commands.

27.3.3. Types of Shells

There are two main shell families in Unix:

27.3.4. Shell Search Paths

If the shell is trying to run a command, and the command isn't built-in to the shell itself, the shell looks in a list of directories called a search path. Unix systems have standard directories with names like /bin and /usr/bin that hold standard Unix programs. Almost everyone's search path includes these directories.

If you do much shell programming, you should make a directory on your account for executable files. Most people name theirs bin and put it under their home directory. See Section 7.4.

27.3.5. Bourne Shell Used Here

Most serious shell programmers write their scripts for the Bourne shell or its variants, such as bash or ksh. So do we.

Newer Bourne shells have features -- such as shell functions (Section 29.11), an unset command for shell variables, and others -- that the earlier Version 7 Bourne shell didn't. Most scripts in this book are written to work on all Bourne shells -- for the sake of portability, some scripts don't use these new features. It's pretty rare to find such old shells around nowadays, though, so use your own judgment. It is pretty unlikely that if you're writing a shell script for your own use on a new system you will ever need to back-port it to run on a V7 system.

For the rest of these introductory articles, it may be easier if you have a terminal close by so you can try the examples. If your account uses the Bourne shell or one of its relatives (ksh, bash, etc.), your prompt probably has a dollar sign ($) in it somewhere, unless you've modified the prompt yourself (Section 4.1). If your account isn't running the Bourne shell, start one by typing sh. Your prompt should change to a dollar sign ($). You'll be using the Bourne shell until you type CTRL-d at the start of a line:

% sh
$
$ ...Enter commands...
$ CTRL-d
%

27.3.6. Default Commands

One more thing to note is that when dealing with shell scripts, which store sequences of commands that you want to be able to run at one time, you will likely need to specify the shell or other program that will run the commands by default. This is normally done using the special #! notation (Section 36.2) in the first line of the script.

#!/bin/sh
# everything in this script will be run under the Bourne shell

...

#!/bin/tcsh
# everything in this script will be run under tcsh

...

#!/usr/bin/perl
# everything in this script will be interpreted as a perl command

...

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.