Variables control shell behavior, including the prompt display, where to look for commands, and whether to maintain a history list. The shell understands two kinds of variables: shell variables and environment variables. In general, shell variables are used internally by the shell and affect its own operation, whereas environment variables are accessible to programs started from the shell. Appendix B, csh and tcsh Quick Reference, lists several useful variables of each type.
Variables can be set directly from the command line, but you'll usually set them in your startup files, either to override default values that the shell supplies, or to define variables that the shell leaves undefined.
Use the set command to define shell variables. Some variables need no explicit value; they have an effect merely by existing:
set notify Turn on immediate notification of job completion set ignoreeof Disable logging out via CTRL-D
Other variables require a value:
set history = 20 Tell shell to remember the last 20 commands set prompt = "% " Define command line prompt set fignore = (.o .bak) Specify suffixes to ignore during filename completion set term = vt100 Specify terminal type
Values that contain spaces or other special characters should be put in quotes, as in the set prompt command above. Values consisting of multiple strings must be surrounded with parentheses, as in the set fignore command.
You don't need to have spaces around the = separating the variable name from the value. If you do, there must be spaces on both sides:
set history= 20 Illegal set history =20 Illegal set history=20 Legal set history = 20 Legal
I prefer to use spaces because I find set commands more readable that way.
Use setenv to define environment variables:
setenv TERM vt100 Specify terminal type setenv PAGER /usr/ucb/more Specify preferred output pager setenv EDITOR /usr/bin/emacs Specify preferred editor
Notice that, unlike set, setenv has no = between the variable name and its value.
Environment variables differ from shell variables in that, although their values are known by the shell, those values are also accessible to programs you run from the shell. (For example, if your mailer allows you to drop into an editor to edit a message, it might determine which editor to use by consulting the EDITOR
environment variable.) Environment variable names are usually in uppercase type. Uppercase is not a requirement, but is a useful convention that helps distinguish environment variables from shell variables.
Use set or setenv with no argument to see the values of all your shell or environment variables:
%set
Display all shell variables %setenv
Display all environment variables
Some systems have env or printenv commands that act like setenv.
A reference to a variable name
is written $
name
or ${
name
}
. You can examine individual variable values using echo:
%echo $TERM $path
Display terminal type and command search path %echo ${TERM} ${path}
This is equivalent to the above command
If you try to use a variable that doesn't exist (i.e., has not been defined), the shell complains:
% echo $junk
junk: Undefined variable.
If a variable refers to a filename, you can apply one of the modifiers shown in Table 4-1 to extract different parts of the value. Modifiers are applied using the syntax $
name
:
m
or ${
name
:
m
}
:
%set xyz = /etc/csh.cshrc
%echo root $xyz:r extension $xyz:e head $xyz:h tail $xyz:t
root /etc/csh extension cshrc head /etc tail csh.cshrc %echo root ${xyz:r} extension ${xyz:e} head ${xyz:h} tail ${xyz:t}
root /etc/csh extension cshrc head /etc tail csh.cshrc
If you want to turn off a shell or environment variable, use unset or unsetenv:
unset mail Turn off notification of new mail unsetenv DISPLAY Turn off X display specification
In tcsh, a shell variable can be made read-only using set –r. A read-only variable cannot be modified with set, or removed with unset:
%set -r xyz
%set xyz
set: $xyz is read-only. %unset xyz
unset: $xyz is read-only.
A system administrator can use read-only variables to place a non-modifiable variable in the working environment of all tcsh users. At a busy site where terminal lines and other resources are at a premium, the following line can be placed in a system-wide startup file to log out idle users after an hour of inactivity:
set -r autologout = 60 Log out idle users after 60 minutes
Some shell variables have a corresponding environment variable, so that setting one member of the pair implicitly sets the other. For instance, you can use either of the following commands to specify your terminal type:
set term = vt100 This also sets the TERM environment variable setenv TERM vt100 This also sets the term shell variable
path and PATH are another such pair, although their values are specified using different formats:
set path = (˜/bin /usr/bin /bin /usr/ucb) setenv PATH /usr/staff/dubois/bin:/usr/bin:/bin:/usr/ucb
When you set either path or PATH, the shell automatically converts the value to the format required by the other.
Get Using csh & tcsh now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.