[Page 298]8.3. Variables The C shell supports local and environment variables. A local variable may hold either one value, in which case it's called a simple variable, or more than one value, in which case it's termed a list. This section describes the C shell facilities that support variables. 8.3.1. Creating/Assigning Simple Variables To assign a value to a simple variable, use the built-in set command (Figure 8-2). Figure 8-2. Description of the set shell command.set {name [ =word ]}* | If no arguments are supplied, a list of all the local variables is displayed. If word is not supplied, name is set to a null string. If the variable name doesn't exist, it is implicitly created. |
Here are some examples: % set flag ...set "flag" to a null string. % echo $flag ...nothing is printed, as it's null. % set color = red ...set "color" to the string "red". % echo $color red % set name = Graham Glass ...beware! Must use quotes. % echo $name ...only the first string was assigned. Graham % set name = "Graham Glass" ...now it works as expected. % echo $name Graham Glass % set ...display a list of all local variables. argv () cdpath /home/glass color red cwd /home/glass flag ... name Graham Glass term vt100 user glass % _ 8.3.2. Accessing a Simple Variable In addition to the simple variable access syntax ($name), the C shell supports the complex access methods listed in Figure 8-3. [Page 299]Figure 8-3. Accessing C shell variables.Syntax | Action |
---|
${name} | Replaced by the value of name. This form is useful if the expression is immediately followed by an alphanumeric that would otherwise be interpreted as part of the variable's name. | ${?name} | Replaced by 1 if name is set and 0 otherwise. |
Here are some examples that illustrate these access methods. In the first example, I used braces to append a string to the value of a variable: % set verb = sing % echo I like $verbing verbing: Undefined variable. % echo I like ${verb}ing I like singing % _ In the following example, I used a variable as a simple flag in a conditional expression: % cat flag.csh ...list the script. # set flag ...set "flag" to a null string. if (${?flag}) then ...branch if "flag" is set. echo flag is set endif % tcsh flag.csh ...execute the script. flag is set % _ 8.3.3. Creating/Assigning List Variables To assign a list of values to a variable, use the built-in set command with the syntax shown in Figure 8-4. Figure 8-4. Description of the set shell command setting a list variable.set {name = ({ word}* ) }* | If the named variable doesn't exist, it is implicitly created. The named variable is assigned to a copy of the specified list of words. |
[Page 300]Here's an example: % set colors = ( red yellow green ) ...set to a list. % echo $colors ...display entire list. red yellow green % _ 8.3.4. Accessing a List Variable The C shell supports a couple of ways to access a list variable. Both of these methods have two forms, the second of which is surrounded by braces. The second form is useful if the expression is immediately followed by an alphanumeric that would otherwise be interpreted as part of the variable's name. Figure 8-5 describes the access methods: Figure 8-5. Accessing C shell list variables.Syntax | Action |
---|
$name[selector] ${name[ selector]} | Both forms are replaced by the element of name whose index is specified by the value of selector. selector may be either a single number, a range of numbers in the format start-end, or a *. If start is omitted, 1 is assumed. If end is omitted, the index of the last element is assumed. If a * is supplied, then all of the elements are selected. The first element of a list has index 1. | $#name ${#name} | Both forms are replaced by the number of elements in name. |
Here are some examples: % set colors = ( red yellow green ) ...set to a list. % echo $colors[1] ...display first element. red % echo $colors[2-3] ...display 2nd and 3rd. yellow green % echo $colors[4] ...illegal access. colors: Subscript out of range. % echo $#colors ...display size of list. 3 % _ 8.3.5. Building Lists To add an element onto the end of a list, set the original list equal to itself plus the new element, surrounded by parentheses; if you try to assign the new element directly, you'll get an error message. The following example illustrates some list manipulations: % set colors = ( red yellow green ) ...set to a list. % set colors[4] = pink ...try to set the 4th. [Page 301]set: Subscript out of range. % set colors = ( $colors blue ) ...add to the list. % echo $colors ...it works! red yellow green blue % set colors[4] = pink ...OK, since 4th exists. % echo $colors red yellow green pink % set colors = $colors black ...don't forget to use (). % echo $colors ...only the first was set. red $ set girls = ( sally georgia ) ...build one list. $ set boys = ( harry blair ) ...build another. $ set both = ( $girls $boys ) ...add the lists. $ echo $both ...display the result. sally georgia harry blair % _ 8.3.6. Predefined Local Variables In addition to the common predefined local variables, the C shell defines those shown in Figure 8-6. Figure 8-6. C shell predefined local variables. [Page 302]Name | Value |
---|
$?0 | 1 if the shell is executing commands from a named file, and 0 otherwise. | $< | The next line of standard input, fully quoted. | $argv | A list that contains all of the positional parameters: $argv[1] is equal to $1. | $cdpath | The list of alternate directories that chdir uses for searching purposes. | $cwd | The current working directory. | $echo | Set if the -x command-line option is active. | $histchars | May be used to override the default history metacharacters. The first character is used in place of ! for history substitutions, and the second is used in place of ^ for quick command re-execution. | $history | The size of the history list. | $home | The shell's home directory. | $ignoreeof | Prevents the shell from terminating when it gets a Control-D. | $mail | A list of the files to check for mail. By default, the shell checks for mail every 600 seconds (10 minutes). If the first word of $mail is a number, the shell uses this value instead. | $noclobber | Prevents existing files from being overridden by >, and nonexistent files from being appended to by >>. | $noglob | Prevents wildcard expansion. | $nonomatch | Prevents an error from occurring if no files match a wildcard filename. | $notify | By default, the shell notifies you of job status changes just before a new prompt is displayed. If $notify is set, the status change is displayed immediately when it occurs. | $path | Used by the shell for locating executable files. | $prompt | The shell prompt. | $savehist | The number of commands to save in the history file. | $shell | The full pathname of the login shell. | $status | The exit code of the last command. | $time | If this is set, any process that takes more than this number of seconds will cause a message to be displayed that indicates process statistics. | $verbose | Set if the -v command-line option is used. |
[Page 302]Here's a small shell script that uses the $< variable to obtain a user response: % cat var5.csh ...list the script. # echo -n "please enter your name: " set name = $< # take a line of input. echo hi $name, your current directory is $cwd % tcsh var5.csh ...execute the script. please enter your name: Graham hi Graham, your current directory is /home/glass % _. 8.3.7. Creating/Assigning Environment Variables To assign a value to an environment variable, use the built-in command setenv (Figure 8-7). Figure 8-7. Description of the setenv shell command.setenv name word | If the named variable doesn't exist, it is implicitly created; otherwise, it is overwritten. Note that environment variables always hold exactly one value; there is no such thing as an environment list. |
[Page 303]Here's an example of setenv: % setenv TERM vt52 ...set my terminal type. % echo $TERM ...confirm. vt52 % _ 8.3.8. Predefined Environment Variables In addition to the common predefined environment variables (Figure 8-8), the C shell supports the ones listed below. Figure 8-8. C shell predefined environment variables.Name | Value |
---|
$LOGNAME | The shell owner's username. |
|