The Bourne shell has a handy set of operators for testing and setting
They're listed in
Table 45.2: Bourne Shell Parameter Substitution Operators
is not set or is empty, use default
is not set or is empty, set it to default
and use that value.
is set and is not empty, use
. Otherwise, use nothing (null string).
is set and is not empty, use its value.
Otherwise, print message
, if any, and exit from the
shell. If message
is missing, print a default message (which depends on your shell).
If you omit the colon (
) from the expressions in Table 45-2, the
shell doesn't check for an empty parameter.
In other words, the substitution will happen whenever the parameter is set.
(That's how some early Bourne shells work:
they don't understand a colon in parameter substitution.)
To see how parameter substitution works, here's another version of the
if cp "$1" "$1.bak"
exit # USE STATUS FROM EDITOR
echo "`basename $0` quitting: can't make backup?" 1>&2
is set and is not empty, its value
) is used and the
command line becomes
isn't set, the command line will default to
You can use parameter substitution operators in any command line.
You'll see them used with the
) operator (45.9
checking or setting default values.
There's an example below.
The first substitution (
) will leave
empty because the variable has been set.
The second substitution will set
because the variable has been set but is empty.
The third substitution will leave
set to stuff
The Korn shell and bash
string editing operators (9.7
They're useful in shell programs,
as well as on the command line and in shell setup files.