The Shell as a Programming Language

Now that you’ve seen some basic shell operations, it’s time to move on to some actual shell programs. There
are two ways of writing shell programs. You can type a sequence of commands and allow the shell to execute
them interactively, or you can store those commands in a file that you can then invoke as a program.
Interactive Programs
Just typing the shell script on the command line is a quick and easy way of trying out small code fragments,
and is very useful while you are learning or just testing things.

Suppose you have a large number of C files and wish to examine the files that contain the string POSIX.
Rather than search using the grep command for the string in the files and then list the files individually,
you could perform the whole operation in an interactive script like this:

$ for file in *
> do
> if grep -l POSIX $file
> then
> more $file
> fi
> done
posix
This is a file with POSIX in it - treat it well
$

Note how the normal $ shell prompt changes to a > when the shell is expecting further input. You can
type away, letting the shell decide when you’re finished, and the script will execute immediately.