DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Customizing your environment

Setting shell variables

To set a variable in the shell, use an equal sign to assign it a value. If the variable does not already exist, it is created. For example:

MYVARIABLE=hello

It is common practice to use all uppercase letters for the name of variables, to distinguish them from UNIX system commands (that are almost always lowercase).

To refer to the value of a variable, prefix the variable's name with a ``$'' symbol. If you omit the ``$'', the shell will assume you are referring to the name of the variable, not its current value. For example, in the Bourne and Korn shells:

   $ MYVARIABLE=hello
   $ echo MYVARIABLE
   MYVARIABLE
   $ echo $MYVARIABLE
   hello
   $
The C shell equivalent of this is as follows:
   % set MY=hello
   % echo MY
   MY
   % echo $MY
   hello
   %
When using the Bourne and C shells, you may sometimes see variable names enclosed in braces ({}) within a reference. The braces are used to delimit the name of the variable. For example echo ${MYVARIABLE} could be used instead of echo $MYVARIABLE. This is particularly useful when you want to concatenate the contents of a variable with another word. For example:
   $ MYVARIABLE=hello
   $ echo $MYVARIABLE
   hello
   $ echo ${MYVARIABLE}_there
   hello_there
   $ echo $MYVARIABLE_there
   

$

In the third echo command, because MYVARIABLE is not separated from ``_there'' the shell tries to substitute a variable called MYVARIABLE_there (which does not exist).


NOTE: If you are using the C shell, an error message is displayed.

It is a good idea to make a habit of enclosing variable names in braces whenever there is any doubt, to reduce the likelihood of this kind of error.


NOTE: The Korn shell does not require braces around environment variables.


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004