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


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