Conditional substitution in the Bourne shell
Normally, the shell replaces the expression $
Variable with
the string value assigned to the Variable variable,
if there is one. However, there is a special notation that allows conditional
substitution, depending on whether the variable is set or not null, or
both.
By definition, a variable is set if it has ever been assigned a value. The value of a variable can be the null string, which you can assign to a variable in any one of the following ways:
Item | Description |
---|---|
A= |
|
bcd="" |
|
Efg='' |
Assigns the null string to the A , bcd ,
and Efg . |
set '' "" |
Sets the first and second positional parameters to the null string and unsets all other positional parameters. |
The following is a list of the available expressions you can use to perform conditional substitution:
Item | Description |
---|---|
${Variable- String} | If the variable is set, substitute the Variable value in place of this expression. Otherwise, replace this expression with the String value. |
${Variable:-String} | If the variable is set and not null, substitute the Variable value in place of this expression. Otherwise, replace this expression with the String value. |
${Variable=String} | If the variable is set, substitute the Variable value in place of this expression. Otherwise, set the Variable value to the String value and then substitute the Variable value in place of this expression. You cannot assign values to positional parameters in this fashion. |
${Variable:=String} | If the variable is set and not null, substitute the Variable value in place of this expression. Otherwise, set the Variable value to the String value and then substitute the Variable value in place of this expression. You cannot assign values to positional parameters in this fashion. |
${Variable?String} | If the variable is set, substitute the Variable value
in place of this expression. Otherwise, display a message of the following
form: and exit from the current shell
(unless the shell is the login shell). If you do not specify a value for the String variable,
the shell displays the following message:
|
${Variable:?String} | If the variable is set and not null, substitute
the Variable value in place of this expression. Otherwise, display
a message of the following form: and
exit from the current shell (unless the shell is the login shell). If you
do not specify the String value, the shell displays the
following message:
|
${Variable+String} | If the variable is set, substitute the String value in place of this expression. Otherwise, substitute the null string. |
${Variable:+String} | If the variable is set and not null, substitute the String value in place of this expression. Otherwise, substitute the null string. |
In conditional substitution, the shell does not evaluate the String variable
until the shell uses this variable as a substituted string. Thus, in the following
example, the shell executes the pwd command only if
d
is
not set or is null:echo ${d:-`pwd`}