Unix Power ToolsUnix Power ToolsSearch this book

35.9. Shell Variables

Shell variables are really just the "general case" of environment variables (Section 35.3). 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 articles 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 (except, possibly, variables in some shell setup files (Section 3.3)). 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 (Section 30.1) variable to determine how many of your previous commands to remember; if the noclobber (Section 43.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 or .tcshrc (Section 3.3) files to define these important variables appropriately. Alternatively, they split them up into context-specific files and then read them into their environment (Section 35.29) as needed.

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

% set name=value   C shell
$ name=value     other shells

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=      other shells

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.

Most newer shells -- but not the original C and Bourne shells -- let you prevent accidental changes in a variable by marking it read-only after you've stored its value:

% set -r name    tcsh
$ readonly name  other shells

(In zsh, you can mark a variable read-only as you initialize it: readonly name=value.) If you want to make the shell forget that a variable ever existed, use the unset command. Note that, in general, you can't unset a read-only variable! Also, older Bourne shells don't have a command like unset:

% unset name   C shell
$ unset name   others except old Bourne shell

If you want to list all of your environment variables, use the command printenv or env (Section 35.3).[105] 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:

[105]printenv and env are external (Section 1.9) 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 "$variablename"

(While the example above gives a C shell prompt, this command works in all Unix shells.) The quotes aren't necessary for something as simple as an echo statement, but if you want the value captured, for example, so that you can apply it to another variable, they are recommended.

Whenever you need the value of a shell variable -- not just with echo -- you need to put a dollar sign ($) in front of the name. Don't use 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 to (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 (Section 35.2).

--ML and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.