Basic Shell Constructs
In this section we introduce the basic building blocks used in just about all shell scripts. You will undoubtedly be familiar with some or all of them from your interactive use of the shell.
Commands and Arguments
The shell's most basic job is simply to execute commands. This is most obvious when the shell is being used interactively: you type commands one at a time, and the shell executes them, like so:
$cd work ; ls -l whizprog.c
-rw-r--r-- 1 tolstoy devel 30252 Jul 9 22:52 whizprog.c $make
...
These examples show the basics of the Unix command line. First, the format is simple, with whitespace (space and/or tab characters) separating the different components involved in the command.
Second, the command name, rather logically, is the first item on the line. Most typically, options follow, and then any additional arguments to the command follow the options. No gratuitous syntax is involved, such as:
COMMAND=CD,ARG=WORK COMMAND=LISTFILES,MODE=LONG,ARG=WHIZPROG.C
Such command languages were typical of the larger systems available when Unix was designed. The free-form syntax of the Unix shell was a real innovation in its time, contributing notably to the readability of shell scripts.
Third, options start with a dash (or minus sign) and consist of a single letter.
Options are optional, and may require an argument (such as cc -o
whizprog
whizprog.c
).
Options that don't require an argument can be grouped together: e.g.,
ls -lt whizprog.c
rather than
ls -l -t whizprog.c ...
Get Classic Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.