next up previous contents
Next: User Defined Variables Up: The C Shell Previous: Aliases

Variables

A variable in the C shell can be any name containing up to 20 letters and digits which starts with a letter or an underscore. A variable is a name or placeholder used to store one or more values. Variables give a high level of generality to everyday interactive shell usage, and are heavily used in shell programming (which will be discussed in a later section). In interactive shell use, a variable can be treated very similar to an alias with the exception that a variable must be referenced (used) with a dollar sign. This dollar sign tells the shell that what follows is a variable, and that the shell should make the appropriate substitution. In order to use a shell variable in the same manner that an alias was used earlier, the long listing can be revisited:

% set ll=''ls -l''
% $ll

Ignoring the set command for a moment, the variable ll is given the text string ls -l as a value, and when the variable is referenced on the following line, the text string is substituted. When the user enters the variable name (preceeded by a dollar sign) followed by a carriage return, the shell interprets the text string as though the user had typed it in himself. The shell does not care how the command line gets entered, it parses it the same whether it was a history subststituion, variable substitution, alias or standard input. The C shell variables can be categroized into five types. These types are as follows:

Before each of these types of variable is covered, the way in which variables are accessed should be examined. While it is true that a variable must be accessed with a $ in front, that is not the end of the story. It should be remembered form the discussion of quoting special characters that a text string enclosed in double quotes will allow the shell to make variable substitutions while a string in single quotes will not. Consider the following example,

% set NAME=''John Smith''
% echo ``His name is $NAME''
His name is John Smith''
% echo 'His name is $NAME'
His name is $NAME

where the single quoted text string is echoed verbatim. Another complication that can arise when accessing variables as part of a longer string is, for example, the variable DAY is assigned the value ``Tues'', and then an attempt is made to make the string ``Tuesday'' containing the variable DAY the following problem arises:

% set DAY=Tues
% echo $DAYday
Dayday: Undefined Variable
%

What has happened here is that there is no whitespace between the variable name and the rest of the string. When the shell parses the command line, it interprets everything following the $ and up until whitespace as the variable name. Since DAYday was not defined as a variable (given a value) the shell displays an error message. To alleviate this problem, curly braces ({}) can be used. If a curly brace follows directly after a $, everything inside of the closed curly brace pair will be considered a variable name.

% set DAY=Tues
% echo ${DAY}day
Tuesday

Variables names are case sensitive so DAY, Day and day are all different. It is not good practice to define more than one variable with the same name but different case. In fact it has become standard for shell variables to be given all upper case characters.

It is also important to note that when assigning a value to a variable, the shell considers everything between the equals sign and the next whitespace to be the value of the variable. It is thus important that no whitespace (other than what is meant to be used) is introduced during a variable assignment. If whitespace is to be used, the appropriate quotation characters must be used.




next up previous contents
Next: User Defined Variables Up: The C Shell Previous: Aliases

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