Bourne Shell Variables

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.

Quoting

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.

Table 3.3. Quoting
Character Description Functionality
Backslash Quotes the next character (also known as “escaping”).
' Single quote marks No interpretation occurs.
" Double quotes All characters enclosed in double quotation marks are quoted except backslash, accent grave, double quote, and the currency symbol.

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.

Delimiters

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.

Table 3.4. Delimiters
Character Description
; Command delimiter. Acts as a Return and executes the commands sequentially.
& Runs commands asynchronously.
( ) Groups commands into a single logical word.
newline Separates records (the default).
Spaces, tabs Separates fields (the default).

Shell Variables

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.

Table 3.5. Default Bourne Shell Variables
Variable Description
HOME Sets the value of the user’s home directory.
LPDEST Sets the user’s default printer.
MAIL If this parameter is set to the name of a mail file and the MAILPATH parameter is not set, the shell informs the user of the arrival of mail in the specified file.
MAILCHECK Specifies how often (in seconds) the shell will check for the arrival of mail in the files specified by the MAILPATH or MAIL parameters. The default value is 600 seconds (10 minutes). If set to 0, the shell will check after each prompt.
MAILPATH Sets the mail path by defining a colon-separated list of filenames. If this parameter is set, the shell informs the user of the arrival of mail in any of the specified files. Each filename can be followed by % and a message that will be printed when the modification time changes. The default message is You have mail.
MANPATH Sets the hierarchies of available man pages.
PATH Lists, in order, the directories that the shell searches to find the program to run when the user types a command. If the directory is not in the search path, users must type the complete pathname of a command. The default PATH is automatically defined and set as specified in .profile (Bourne or Korn shell) or .cshrc (C shell) as part of the login process.
PS1 Sets the primary prompt string, by default "$ ".
PS2 Sets the secondary prompt string, by default "> ".
SHELL Sets the default shell used by make, vi, and other tools. When the shell is invoked, it scans the environment for this name.
TERM Defines the terminal. This variable should be reset in /etc/profile or /etc/.login. When the user invokes an editor, the system looks for a file with the same name as the definition of this environment variable. The system searches the directory referenced by TERMINFO to determine the terminal characteristics.
TERMINFO Specifies the pathname for an unsupported terminal that has been added to the terminfo database. Use the TERMINFO variable in $HOME/.profile or /$HOME/.login. When the TERMINFO environment variable is set, the system first checks the TERMINFO path defined by the user. If it does not find a definition for a terminal in the TERMINFO directory defined by the user, it searches the default directory, /usr/share/lib/terminfo, for a definition. If it does not find a definition in either location, the terminal is identified as “dumb.”
TZ Sets the time zone used to display dates (for example, in the ls -l command). If TZ is not set in the user’s environment, the system setting is used; otherwise, Greenwich Mean Time is used.
USER Defines the name of the user currently logged in. The default value of USER is automatically set by the login program to the username specified in the /etc/passwd file. You should only need to reference (not reset) this variable.

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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset