next up previous contents
Next: Command history (history) Up: The Shell (csh, tcsh, Previous: Environment variables (setenv, printenv)   Contents

Variable substitution (set, echo)

The shells allow you to create ordinary variables as well. A variable is simply a named location in which to store text. It is composed of a name and a value. A variable name can be up to twenty characters long and must start with a letter. A dollar sign followed by a variable name is replaced with the contents of the variable by the shell. The format to assign a value to a variable is:

set variable_name = variable_value                 (C-shells)

You can clear this value by using the unset command.

You can assign the output of a command to a variable by using backwards single quotes around the command itself. For example,

malama% set prompt = ```hostname`% ''

will set up your prompt to include the name of the system you are logged onto, which is obtained using the command hostname.

Variables can come in very handy. For instance, if there is a directory which has a long pathname, that you use quite often, then you could set up a variable for it. E.g.:

malama% set text = /usr/dict/papers Now, if you wish to refer to this directory you can use the variable name instead:

malama% cd $text
/usr/dict/papers

To show the contents of a variable use the echo command in the following form:

malama% echo $text
/usr/dict/papers

Note how similar variable substitutions, both environmental and others, are to logical names in VMS.

There are some special variables in the shells. In the C-shells, for example, there is ``prompt'' (see above), and there is ``echo'' (no connection to echo described above). If you put the command set echo at the beginning of a shell script, every command will be echoed to the standard output. Be sure to put unset echo at the end if you are going to be executing more commands, or everything you type after running the script will also be echoed.


next up previous contents
Next: Command history (history) Up: The Shell (csh, tcsh, Previous: Environment variables (setenv, printenv)   Contents
h.stilmack@jach.hawaii.edu