next up previous contents
Next: Parameters Up: Variables Previous: Variables

Variable Substitutions

Once a variable has been assigned a value, it can be easily referenced by the use of a dollar sign in front of the variable name as shown above. A user may wish to append a string directly to a string variable such as

$ VAR=Tues
$ echo $VARday

which will return no value since no value has been assigned to VARday. The shell can not determine that the user meant to treat $VAR as a separate entity and hence it treated the the string as one complete variable name. This is because the shell uses white space when interpreting variable 22substitutions. However, if curly braces containing the variable name are placed within a string, the shell will handle the entire string as desired:

$ VAR=Tues
$ echo ${VAR}day
Tuesday

The Bourne shell also provides variations on the above variable substitution that allow alternative substitutions under certain conditions. There are four such conditional substitutions in the Bourne shell which are listed in table 2.2:

  table80
Table 2.2:  Conditional variable substitutions.

For example, if the user wants to have an error message printed when a variable gets assigned a value, he could do the following:

$ ERROR=TRUE
$ echo ${ERROR:+"An error has been detected"}
An error has been detected



Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998