The shell lets you define variables to perform calculations, and to pass information between commands and applications. They are either integers or strings, however it is possible to perform floating-point arithmetic by using certain applications in your script (see Section 10.3). You don’t need to declare the data type explicitly; the type is determined when you assign a value to a variable.
Variables are only defined in the current process. If you want global variables, i.e. ones that are
available to all processes, you should assign them in your
Variable names comprise up to 20 letters, digits, and underscores; and should begin with a letter or underscore.
You assign a value to a variable using the set command.
flux_100is not a real number. Multi-word strings should be enclosed in
"quotes. The spaces around the equals sign are necessary.
You can also remove variables with the unset command. This accepts
*?[ ]wildcards to match the
names of the shell variables.
The set command is again used but the elements are space-separated lists enclosed by parentheses. Somewhat surprisingly for a shell that mimics C, array elements start at 1, like Fortran. Here are some illustrations.
"blue", the second is "yellow" and so on. Multi-word elements must be in
"quotes. So the first element of
"Count rate", and the second is
"Frequency". The seventh element of
To obtain the value of a variable, you prefix the variable’s name with the dollar sign.
Number of runs is 333to standard output.
The most distant quasar observed is at redshift 5.1.
nis greater than ten, and if it is, executes the statement within the if…endif structure. The value is also passed into an integer parameter of the application mem2d; and a string parameter of application display, so if
nwere forty, the resultant value would be
"Result of deconvolution after 40 iterations".
Arithmetic, logical and string operations are presented in Sections 10 and 11.
The values of array elements are selected by appending the element index in brackets. This
would echo to standard output the string
Starting search at co-ordinates x=300,
The following illustrates use of a text array to specify the annotations of a plot created with application linplot.
There are some shorthands for specifying subsets of an array. The best way to describe them
is through some examples. All the values assume
set prime = (2 3 5 7 11 13 17 19
| || |
Number of elements of variable
| || |
All elements of variable
| || |
The last element of variable
| || |
The third to fifth elements of variable
| || |
The eighth to last elements of variable
Here we bring some of these ingredients together. Suppose we are experimenting trying to find the
most suitable reddish background (
palnum=0) colour for image display using the palentry command
from (SUN/95). The script below loops using a while...end construct: all the commands inside the
construct are repeated until the condition following the while is satisfied. That might seem a little odd
here as we appear to repeat the same commands ad infinitum because the number of colours
is fixed. That doesn’t happen because of the C-shell shift command. It discards the first
element of an array and reduces the indices of the remaining elements by one, so the original
$colours and so on. This means that the chosen background colour
is always $colours. The shift also decrements the number of colours by one. So the
script changes the background colour and pauses for five seconds for you to consider the
The shell has a number of special characters otherwise known as metacharacters. These include
wildcard characters such as
*?[ ], single and double quotes, parentheses, and the \ line
If you assign a value to a variable or application parameter which includes one or more of these
metacharacters, you need to switch off the special meanings. This operation is called escaping. Single
characters may be escaped with a backslash. For multiple occurrences single quotes
’ ’ will pass the
enclosed text verbatim. Double quotes
" " do the same as single quotes except that
$, \, and left quote
retain their special meaning.
acceptkeyword. So we have to tell the shell to treat the backslash literally by preceding it with backslash!
In the last pair of examples an NDF section (see SUN/95’s chapter called “NDF Sections”) is
specified. As the last contains a variable value to be substituted, the
$ retains its normal
special meaning but the parentheses are escaped by surrounding the section in double
Some scripts might have parameters that cannot be defaulted, and so if their values are not given on the command line, the script needs to prompt for them.
The C-shell has some special forms of variable. We have seen some already: the
$< for prompting, and
how to specify array elements. The remainder largely concern command-line arguments. Here they
The name of the script being run
The value of the
The value of the
The number of arguments passed to the script
All the arguments supplied to the script
Process identification number (useful for making temporary files with unique names)
$#argv. There is no practical limit to the number of arguments you can supply.
Let’s look at a few examples. Below is a script
You can see an example using
$* to process a list of files in Section 12.2.2. There are other examples of
script arguments in Section 14.
The C-shell has some predefined variables. We’ve already met argv. The most useful other ones are listed below.
Current working directory
List of directories in which to search for commands
The status returned by the last command, where
So if you wanted to include details of the user and directory at the head of some data-processing log you might write this in your script.
logfilesaying who ran the script, and then appends details of the directory in which processing occurred. Note the different metacharacters, namely
>redirects the output to a file, and
»appends to an existing file.