Text-only Table of Contents (frame/ no frame)
(9) Variables Previous Top Next

Shell Variables

Scripts are not very useful if all the commands and options and filenames are explicitly coded. By using variables, you can make a script generic and apply it to different situations. Variable names consist of letters, numbers and underscores ([a-zA-Z0-9_], cannot start with a number, and are case sensitive. Several special variables (always uppercase names) are used by the system -- resetting these may cause unexpected behaviour. Some special variables may be read-only. Using lowercase names for your own variables is safest.

Setting and exporting variables

Creates (if it didn't exist) a variable named "srcfile" and sets it to the value "dataset1". If the variable already existed, it is overwritten. Variables are treated as text strings, unless the context implies a numeric interpretation. You can make a variable always be treated as a number. Note there must be no spaces around the "=".
Display all the variables currently set in the shell
unset srcfile
Remove the variable "srcfile"
Give the variable a null value, (not the same as removing it).
export srcfile
Added srcfile to the list of variables which will be made available to external program through the environment. If you don't do this, the variable is local to this shell instance.
List all the variables currently being exported - this is the environment which will be passed to external programs.

Using variables

Prefacing the variable name with $ causes the value of the variable to be substituted in place of the name.
If the variable is not surrounded by whitespace (or other characters that can't be in a name), the name must be surrounded by "{}" braces so that the shell knows what characters you intend to be part of the name.


# Tries to find $datafile_part1, which doesn't exist
echo $datafile_part1.sas 
# This is what we intended
echo ${datafile}_part1.sas  

Conditional modifiers

There are various ways to conditionally use a variable in a command.
Substitute the value of $datafile, if it has been defined, otherwise use the string "default". This is an easy way to allow for optional variables, and have sensible defaults if they haven't been set. If datafile was undefined, it remains so.
Similar to the above, except if datafile has not been defined, set it to the string "default".
If variable datafile has been defined, use the string "default", otherwise use null. In this case the actual value $datafile is not used.
${datafile?"error message"}
Substitute the value of $datafile, if it has been defined, otherwise display datafile: error message. This is used for diagnostics when a variable should have been set and there is no sensible default value to use. More detail

Variable assignment command prefix

It is possible to export a variable just for the duration of a single command using the syntax:
var=value command args

Previous Top Next

variables.src  last modified Feb 10, 2005 Introduction Table of Contents
(frame/no frame)
(single file)
© Dartmouth College