Previous | Table of Contents | Next |
Variable substitution enables the shell programmer to manipulate the value of a variable based on its state. Variable substitution falls into two categories:
The actions range from one time value substitution to aborting the script.
These categories are broken down into four forms of variable substitution. You can use variable substitutions as shorthand forms of expressions that would have to be written as explicit if-then-else statements, covered in Chapter 10, Flow Control. Table 8.2 provides a summary of all variable substitution methods.
Form | Description |
---|---|
${parameter:-word} | If parameter is null or unset, word is substituted for parameter. The value of parameter does not change. |
${parameter:=word} | If parameter is null or unset, parameter is set to the value of word. |
${parameter:?message} | If parameter is null or unset, message is printed to standard error. This checks that variables are set correctly. |
${parameter:+word} | If parameter is set, word is substituted for parameter. The value of parameter does not change. |
The first form enables a default value to be substituted when a variable is unset or null. This is formally described as
${parameter:-word}
Here parameter is the name of the variable, and word is the default value. A simple example of its use is
PS1=${HOST:-localhost}"$ " ; export PS1 ;
You could use this in a users .profile to make sure that the prompt is always set correctly. This form of variable substitution does not affect the value of the variable. It performs substitution only when the variable is unset.
To set the value of a variable, the second form of variable substitution must be used. This form is formally described as:
${parameter:=word}
Here, parameter is the name of the variable, and word is the default value to set the variable to if it is unset. Appending the previous example, you have
PS1=${HOST:='uname -n'}"$ " ; export PS1 HOST ;
After the execution of this statement, both HOST and PS1 are set. This example also demonstrates the fact that the default string to use does not have to be a fixed string but can be the output of a command. If this substitution did not exist in the shell, the same line would have to be written as
if [ -z "$HOST" ] ; then HOST='uname n' ; fi ; PS1="$HOST$ "; ⇒export PS1 HOST ;
As you can see, the variable substitution form is shorter and clearer than the explicit form.
Sometimes substituting default values can hide problems, thus sh supports a third form of variable substitution that enables a message to be written to standard error when a variable is unset. This form is formally described as
${parameter:?message}
A common use of this is in shell scripts and shell functions requiring certain variables to be set for proper execution. For example, the following command exits if the variable $HOME is unset:
: ${HOME:?"Your home directory is undefined."}
In addition to using the variable substitution form described previously, you are also making use of the no-op (no-op as in no operation) command, :, which simply evaluates the arguments passed to it. Here you are checking to see whether the variable HOME is defined. If it is not defined, an error message prints.
The final form of variable substitution is used to substitute when a variable is set. Formally this is described as
${parameter:+word}
Here parameter is the name of the variable, and word is the value to substitute if the variable is set. This form does not alter the value of the variable; it alters only what is substituted. A frequent use is to indicate when a script is running in debug mode:
echo ${DEBUG:+"Debug is active."}
Previous | Table of Contents | Next |