One of the great things about UNIX is that it's made up of individual
utilities, "building blocks" like cat
, that you run
from a shell prompt.
Using pipes, redirection, filters, and so on, you can combine those
utilities to do an incredible number of things.
Shell programming lets you take the same commands you'd type at a shell
prompt - and put them into a file you can run by just typing its name.
You can make new programs that combine UNIX programs (and other shell
scripts) in your own way to do exactly what you need.
If you don't like the way a program works, you can write a shell script
to do just what you want.
Because many UNIX users use the shell every day, they don't need to
learn a whole new language for programming... just some tips and
In fact, this chapter covers a lot of programming techniques that
you'll want to use even when you aren't
programming. For example, loops and tests are handy on the
(This series of articles does assume that you've written
programs in some language before, or are generally familiar with
If you haven't, you might start with a more comprehensive shell
Some of the topics you need to learn about as a beginning shell
programmer have already been covered in other chapters. Here are the articles you'll probably want to read - in an order
that makes sense if you're looking for something of a tutorial:
To see how to write a simple shell program, article
To embed scripts from other languages like sed
a shell script, article
For explanation of shells in general, article
To test how your system executes files so you'll know how to write your
shell programs, article
To read about environment and shell variables, articles
command is covered by article
Shell quoting is explained in article
Test strings with a case
Match patterns in a case
Use the output of one command as arguments to another command
with command substitution, article
Find out whether a program worked or failed with its exit status,
Test a program's exit status and do different things if it worked or failed,
Loop through a set of commands and use another command to control that loop,
Set exit status of a shell (shell script), article
Handle interrupts (like CTRL-c) and other signals, article
Read input from the keyboard, article
Handle command-line arguments (options, filenames, etc.), article
Step through arguments, or any list of words, with a for
Handle arguments with the while
Handle command-line arguments in a more standard and portable way
Set shell options and command-line arguments with the set
Test files and strings of characters with the test
Pick a name for a new command with no conflict, article
Find the name of a program and use it in the script, article
Use "subprograms" that can change the current environment, article
This chapter discusses only Bourne shell programming.
In most cases, the C shell
isn't great for shell programming (47.2
A note about command versions:
unfortunately, the same commands on different versions of UNIX can
have different options.
Some Bourne shells are a little different from others.
For instance, some
commands have a -x
option to test for an
executable file; others don't.
commands use a -n
option to mean "no newline at the
end of this string"; others have you put
at the end of the string.
And so on.
Where there are differences, these articles generally use the commands in
If a command doesn't seem to work on your system, check its online
manual page or the sh