home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 6.7 What Time Is It in Japan? Chapter 6
Shell and Environment Variables
Next: 6.9 Special C Shell Variables
 

6.8 Shell Variables

Shell variables are really just the "general case" of environment variables (6.1 ) . If you're a programmer, remember that a UNIX shell really runs an interpreted programming language. Shell variables belong to the shell; you can set them, print them, and work with them much as you can in a C program (or a FORTRAN program or a BASIC program). If you're not a programmer, just remember that shell variables are pigeonholes that store information for you or your shell to use.

If you've read the section on environment variables, you realize that we defined them in exactly the same way. How are shell variables different from environment variables? Whenever you start a new shell or a UNIX program, it inherits all of its parent's environment variables. However, it does not inherit any shell variables; it starts with a clean slate. If you're a programmer, you can think of environment variables as "global" variables, while shell variables are "local" variables. By convention, shell variables have lowercase names.

Just as some programs use certain environment variables, the shell expects to use certain shell variables. For example, the C shell uses the history (11.1 ) variable to determine how many of your previous commands to remember; if the noclobber (13.6 ) variable is defined, the C shell prevents you from damaging files by making mistakes with standard output. Most users insert code into their .cshrc files (2.2 ) to define these important variables appropriately.

To set a shell variable, use one of these commands:

% set 

name

=

value   

C shell

$ name

=

value   

Bourne shell


As a special case, if you omit value , the shell variable is set to a "null" value. For example, the following commands are valid:

% set 

name   

C shell

$ name

=   

Bourne shell


This is important: giving a variable a null value is not the same as deleting the value. Some programs look at variables to see whether or not they exist; they don't care what the actual value is, and an empty value is as good as anything else. If you want to make the shell forget that a variable ever existed, use the unset command. Unfortunately, older Bourne shells don't have a command like unset :

% unset 

name   

C shell

$ unset 

name   

Bourne shell

If you want to list all of your environment variables, use the command printenv (Berkeley UNIX) or env (System V). [1] If you want to list all of your Bourne or C shell variables, just type set . Here's a typical report in the C shell:

[1] printenv and env are external (1.10 ) commands; they work with any shell.

% set


argv    ()
cwd     /home/los/mikel/power/articles
history 40
home    /home/los/mikel
noclobber
path    (/home/los/mikel/bin /usr/local/bin /usr/ucb /bin /usr/bin .)
prompt  los%
shell   /bin/csh
status  0
term    sun
user    mikel

If you want to print the value of an individual variable, give the command:

% echo "$

variable-name

"

(While the example above gives a C shell prompt, this command works in all UNIX shells.)

Whenever you need the value of a shell variable - not just with echo (8.6 ) - you need to put a dollar sign ($ ) in front of the name. You don't need the dollar sign when you're assigning a new value to a shell variable. You can also stick curly braces ({} ) around the name, if you want (e.g., ${ name } ); when you're writing shell programs, this can often make your code much clearer. Curly braces are mostly used when you need to separate the variable name from what comes after it.

But that's getting us out of the range of interactive variable use and into shell programming (44.1 ) .

- ML


Previous: 6.7 What Time Is It in Japan? UNIX Power Tools Next: 6.9 Special C Shell Variables
6.7 What Time Is It in Japan? Book Index 6.9 Special C Shell Variables

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System