Shell Variables   «Prev  Next»
Lesson 6Variable interpretation
ObjectiveExplain how syntax affects the ways variables can be interpreted.

How Syntax Affects Variable Interpretation in Unix Scripts

  1. Variables in shell scripts can have any name that you choose.
  2. The values of variables are also used within many different commands.
At times these two facts can conflict with each other.

Avoiding ambiguous variable names

For example, suppose that you create a variable named DATADIR to hold the name of a data directory. This directory name must be combined with other information in a message printed to the screen. The command you need looks like this:

echo The data directory is /opt/$DATADIR1999/archive

When the value of DATADIR is “ticketing,” you want this command to print the directory /opt/ticketing1999/archive. This is reasonable, but how can the shell tell where the name of the variable ends?
To avoid ambiguity in such cases, the name of the variable is enclosed in braces. The braces are not printed, but they delimit the variable so that the value of the variable can be substituted. This format must always begin with a dollar sign to indicate that you want to substitute a variable with the name that follows. The revised command would be:

echo The data directory is /opt/${DATADIR}1999/archive

Setting and unsetting variables

You have seen that you can define a new variable by simply assigning it a value, as is shown below:

DATADIR=ticketing

In some cases, you may want a variable to cease to exist. Tests in your script may check for the existence of a certain variable. To make the script act correctly, you do not just want the variable to have a different value, you want to remove that variable from the script. The unset command does this. If you use this command:
unset DATADIR

The DATADIR variable will no longer exist in the current script.

Using variable substitution

The braces used to define a variable name explicitly can also be used to substitute different information in the same location based on the status of the variable. In effect, this allows you to perform a test on whether the variable is defined and returns a value based on the results of that test.
For example, if a variable has been unset at some point in the script, you can substitute a default value rather than the value of a variable that is not set. The syntax for this is ${variable:-defaultvalue}. Consider this command:

echo For flight information call ${VENDORPHONE:
-1-800-555-1212}

If the VENDORPHONE variable is undefined, the echo command prints the 1-800 number shown in place of the variable. The value included in this format can be anything you choose. It has no distinct relation to a previous value of the variable you are testing. This test says, in effect, “If the VENDORPHONE variable isn’t set, substitute this value as a backup.” The above command would display the following if VENDORPHONE is undefined when the echo command is executed:

For flight information call 1-800-555-1212

The following MouseOver Tooltip shows the different variable substitution commands.
Basic substitution returns the value of the variable enclosed in braces.
  1. Basic substitution returns the value of the variable enclosed in braces.
  2. Returns the value of 1-800-555-1212 if the variable PHONE has no value (is not set).
  3. If the variable PHONE has no value (is not set), this statement returns the value of 1-800-555-1212 and sets the value of PHONE to 1-800-555-1212.
  4. Returns the string "Approved" if the variable CREDIT_OK is set; otherwise a blank string is returned.
  5. Returns the length of the string contained in the variable NAME.
  6. Returns the error message text "A name is not assigned&#quot; if the variable NAME has no value (compare to using the :- notation).

Using Variable Substitution
Although you can use any word for a variable name, be careful about using variable names that are the same as command names. Though this can usually be made to work, it can confuse both you and the shell. Avoid the risks and use variable names that do not match other system keywords.

Using Variable Substitution - Syntax

Click the link below to check your knowledge of variable substitutions.
Using Variable Substitution - Syntax
The next lesson describes how to use strings as variable values.