A variable is a name that refers to a temporary storage area in memory. A value such as a text string or number is assigned to a variable and can be changed at any time. The Bourne shell uses two types of variables to store values: local and environmental. Each is described in this chapter.
A variable either is set to some particular value or is said to be “unset,” which means it does not exist as a variable. Shell variables are an integral part of shell programming. The shell variable provides the capability to store and manipulate information within a shell program. The variables you use are completely under your control, and you can set or unset any number of variables as needed to perform a particular task.
A variable name must begin with a letter and can contain letters, digits, and underscores but no special characters. In the Bourne shell, environment variables are set with an assignment of NAME=value. In the following example, the ME and BC variables are set by entering the following at a command prompt:
ME=bill BC="bill calkins"
Be sure not to have any whitespace before or after the equals sign (=). Double quotes, as used in the second line of the example, are used when whitespace is present in the text string you are assigning to the variable. Whenever the shell sees a $VARIABLE such as $ME, it substitutes into the command line the value stored for that variable.
Unfortunately, other programs also use many of the special characters that the shell uses—there simply are not enough characters to go around. When the special characters shown in Table 3.3 are used in the shell, they must be quoted. Quoting is used when an assigned value contains a special character, spaces, tabs, or newlines. Without the quotation marks, the special symbols will be interpreted as shell metacharacters instead of being passed as arguments to programs. The three methods of quoting used in the Bourne shell are described in Table 3.3.
Commands are read from the string between two back ticks (` `). The standard output from these commands can be used to set a variable.
Note
Don’t confuse the back tick with the single-quote character. A back tick is the symbol that appears on the same keyboard key as the ~ (tilde). The single-quote character appears on the same key as the “ (double-quote) character.
With the back tick, no interpretation is done on the string before the string is read, except to remove backslashes () used to escape other characters. Escaping back ticks allows nested command substitutions like this one:
font=`grep font `cat filelist``
The backslashes inside the embedded command (`cat filelist`) protect the back ticks from immediate interpretation by the shell. With the backslash (), the back tick just before cat fails to match the initial one before grep. The two “escaped” back ticks will be interpreted on the second pass, and the command will execute correctly.
In other words, because this example has two sets of back ticks, we need to show the shell which back tick pairs up with the other. We do this by using the backslash (). The shell will look at the command string twice— once to pair up the back ticks and a second time to run the command.
Some characters naturally act as delimiters in the Bourne shell. When encountered, such characters have the effect of separating one logical word from the next. The characters outlined in Table 3.4 have a special meaning to the shell and cause termination unless quoted.
To display the value of a variable, enter the following at the command prompt. (The dollar sign informs the shell that the following name refers to a variable.)
echo $BC
The following is displayed:
bill calkins
Variables you set are local to the current shell unless you mark them for export.Variables marked for export are called environment variables and will be made available to any commands that the shell creates. The following command marks the variable BC for export:
export BC
You can list local variables by typing the set command. You can list variables that have been marked for export by typing the env command. The Bourne shell has predefined variables, some of which are described in Table 3.5. These variables are assigned automatically when a user logs in and are defined by the login program, the system initialization file, and the user’s initialization files.
Note
A dumb terminal is a generic terminal type with minimal capabilities, which can be used when the real terminal is unknown. This type of terminal is used as a last resort, but will allow programs other than screen editors to function.
Note
The order of the search path is important. When identical commands exist in different locations, the first command found with that name is used. For example, suppose PATH is defined for user jean (in Bourne and Korn shell syntax) as PATH=/bin:/usr/bin:/usr/sbin:$HOME/bin and a file named sample resides in both /usr/bin and /home/jean/bin. If the user types the command sample without specifying its full pathname, the version found in /usr/bin is used.