Previous Table of Contents Next

Remember all of the Special Parameters can be used also.

C Shell
Same as ksh. The csh allows entire array assignments to variables in one assignment. For example, myvar=( sub1 sub2 sub3 sub4 ) creates 4 elements in the array myvar.
Same as ksh.
$var[*] Same as ksh.
Expands to the values of the elements from subscript n through subscript m
Expands to the value of the last element of array var.
Expands to the number of words stored in the variable.
Substitutes the value 1 if var is set, otherwise the value of 0 is substituted.
$n Same as ksh. The last argument can be accessed by $argv[$#var].

The csh can also modify the values of variables. The following list of modifiers may be added to the end of a variable to modify the value. The variable and modifier must be separated with a colon (:). For the following assume the value of VAR is /tmp/afile.ext.

:e Remove everything but the suffix. Like using expr $var : '.*\.\(.*\)' but much faster and easier to remember. So ${VAR:e} returns ext.
:h Remove the last filename or directory from the path. Like the dirname command. Thus ${VAR:h} returns /tmp.
:q Quote the substituted words, preventing further substitutions. This prevents filename expansion and provides the same effect as placing double quotes around the variable. Should be used with arrays (wordlists).
:r Remove suffix of the form ".xxx" leaving the basename. Somewhat like basename ${VAR} suffix, except the path is not removed. The ${VAR:r} substitution returns /tmp/afile.
:t Remove all leading components, leaving the basename. Like the basename ${VAR} command. So ${VAR:t} returns afile.ext.
:x Same as q, except words are delimited by spaces, tabs, and new-lines. It basically takes a value containing white space and converts it into separate elements of an array (a wordlist).
:gh Globally perform :h on each element of the array variable.
:gr Globally perform :r on each element of the array variable.
:gt Globally perform :t on each element of the array variable.


Variables are used to store data to be used later. They are often used to pass information to programs or shell scripts.

Arrays are used to store related information in a single variable that has multiple elements. They are best utilized when the information you need to store can be accessed in sequential increments. If you have 100 values of donated money that you need to work with multiple times in a shell script, you would store them in an array. Then accessing the values becomes easy because you don't have to remember or code for 100 different variable names. You just use one array name.


In this activity you use VARIABLES and ARRAYS to store and retrieve data, directory paths, and commands. Begin at the shell prompt.

1.  Type PS1="`uname`-${LOGNAME}>" and press Return. Notice in the following display your prompt has changed to the name of your UNIX System, followed by your login name and a ">". If it did not change, type export PS1 and press Return.
     cj> PS1="`uname`-${LOGNAME}>"
     host1-mylogin> _

C Shell
Type set prompt="`hostname`-${USER}>" and press Return.

2.  Type l="ls -C" and press Return.

C Shell
Type set l="ls-C" and press Return.

3.  Then type $l and press Return. Notice the output of ls -C is returned to your terminal. The shell expanded the $l into ls -C and executed it.
4.  Type arr[1]="This is one element of an array!" and press Return to store a string in element one of array "arr." This only works for ksh.

C Shell
Type set arr=("This is one element of an array\!") and press Return.

5.  Type echo ${arr[1]} to display the string you stored in step 3.
6.  Turn to Module 54 to continue the learning sequence.

Previous Table of Contents Next