home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 44.2 Writing a Simple Shell Program Chapter 44
Shell Programming for the Uninitiated
Next: 44.4 Testing How Your System Executes Files
 

44.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 shells, including discussion about how shells run, how they search for programs, and how they read shell script files.

44.3.1 How Shells Run Other Programs

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

44.3.2 Interactive Use vs. 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 (44.2 ) or shell program. The shell handles the shell script just as it handles the commands you type from a terminal (though it doesn't print the % or $ prompts). 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.

44.3.3 Types of Shells

There are two main kinds of shells in UNIX:

  • The C shell (csh ) is especially good for working on a terminal. csh will read shell scripts and has some useful features for programmers. Unfortunately, it has some quirks (47.2 ) that can make shell programming tough.

  • The Bourne shell (sh ) and shells like it are probably used more often for shell programming. (Some newer sh -like shells, including ksh and bash (1.8 ) , combine handy interactive C shell-like features with Bourne shell syntax.)

44.3.4 Shell Search Paths

As article 8.7 explains, if the shell is trying to run a command and the command isn't built-in to the shell itself, it 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 has 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 the home directory. See article 4.2 .

44.3.5 Bourne Shell Used Here

Most serious shell programmers write their scripts for the Bourne shell. So do we.

Newer Bourne shells have features - like shell functions (10.9 ) , 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, though - for portability, the scripts don't use these new features.

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. 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]

%

- JP


Previous: 44.2 Writing a Simple Shell Program UNIX Power Tools Next: 44.4 Testing How Your System Executes Files
44.2 Writing a Simple Shell Program Book Index 44.4 Testing How Your System Executes Files

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System