This subsection describes the following:
Variable substitution
Variable modifiers
Predefined shell variables
Example .cshrc file
Environment variables
In the following substitutions, braces ({}
) are optional,
except when needed to separate a variable name from following characters
that would otherwise be a part of it.
${var } | The value of variable var . |
${var [i ]} | Select word or words in position |
${#var } | The number of words in var . |
${#argv} | The number of arguments. |
$0 | Name of the program. |
${#argv[n ]} | Individual arguments on command line (positional parameters).
|
${n } | Same as ${argv[n ]}. |
${#argv[*]} | All arguments on command line. |
$* | Same as $argv[*]. |
$argv[$#argv] | The last argument. |
${?var } | Return 1 if var is set; 0 if var is not set. |
$$ | Process number of current shell; useful as part of a filename for creating temporary files with unique names. |
$?0 | Return 1 if input filename is known; 0 if not. |
$< | Read a line from standard input. |
Sort the third through last arguments (files) and save the output in a unique temporary file:
sort $argv[3-] > tmp.$$
Process .cshrc commands only if the shell is interactive (i.e., the prompt variable must be set).
if ($?prompt) then set commands, alias commands, etc. endif
Except for $?var
, $$, $?0, and $<,
the variable substitutions above may be followed by one of these
modifiers.
When braces are used, the modifier goes inside them.
Return the variable's root.
Return the variable's extension.
Return the variable's header.
Return the variable's tail.
Return all roots.
Return all extensions.
Return all headers.
Return all tails.
Quote a wordlist variable, keeping the items separate. Useful when the variable contains filename metacharacters that should not be expanded.
Quote a pattern, expanding it into a wordlist.
The table below shows the use of pathname modifiers on the following variable:
aa=(/progs/num.c /book/chap.ps)
Variable Portion | Specification | Output Result |
---|---|---|
Normal Variable | echo $aa | /progs/num.c /book/chap.ps |
Second Root | echo $aa[2]:r | /book/chap |
Second Header | echo $aa[2]:h | /book |
Second Tail | echo $aa[2]:t | chap.ps |
Second Extension | echo $aa[2]:e | ps |
Root | echo $aa:r | /progs/num /book/chap.ps |
Global Root | echo $aa:gr | /progs/num /book/chap |
Header | echo $aa:h | /progs /book/chap.ps |
Global Header | echo $aa:gh | /progs /book |
Tail | echo $aa:t | num.c /book/chap.ps |
Global Tail | echo $aa:gt | num.c chap.ps |
Extension | echo $aa:e | c /book/chap.ps |
Global Extension | echo $aa:ge | c ps |
%set a="[a-z]*" A="[A-Z]*"
%echo "$a" "$A"
[a-z]* [A-Z]* %echo $a $A
at cc m4 Book Doc %echo $a:x $A
[a-z]* Book Doc %set d=($a:q $A:q)
%echo $d
at cc m4 Book Doc %echo $d:q
[a-z]* [A-Z]* %echo $d[1] +++ $d[2]
at cc m4 +++ Book Doc %echo $d[1]:q
[a-z]*
Variables can be set in one of two ways, by assigning a value:
setvar
=value
or by simply turning them on:
setvar
In the table below, variables that accept values are shown with the equal sign followed by the type of value they accept; the value is then described. (Note, however, that variables such as argv, cwd, or status are never explicitly assigned.) For variables that are turned on or off, the table describes what they do when set. The C shell automatically sets the variables argv, cwd, home, path, prompt, shell, status, term, and user.
# PREDEFINED VARIABLES set path=(~ ~/bin /usr/ucb /bin /usr/bin . ) set mail=(/usr/mail/tom) if ($?prompt) then # settings for interactive use set echo set filec set noclobber ignoreeof set cdpath=(/usr/lib /usr/spool/uucp) # Now I can type cd macros # instead of cd /usr/lib/macros set fignore=.o # ignore object files for filec set history=100 savehist=25 set prompt='tom \!% ' # includes history number set time=3 # MY VARIABLES set man1="/usr/man/man1" # lets me do cd $man1, ls $man1 set a="[a-z]*" # lets me do vi $a set A="[A-Z]*" # or grep string $A # ALIASES alias c "clear; dirs" # use quotes to protect ; or | alias h "history|more" alias j jobs -l alias ls ls -sFC # redefine ls command alias del 'mv \!* ~/tmp_dir' # a safe alternative to rm endif
The C shell maintains a set of environment variables
,
which are distinct from shell variables and aren't really part of
the C shell.
Shell variables are meaningful only within the current shell, but
environment variables are automatically exported,
making them available globally.
For example, C shell variables are accessible
only to a particular script in which they're defined, whereas
environment variables can be used
by any shell scripts, mail utilities, or editors you might invoke.
Environment variables are assigned as follows:
setenvVAR
value
By convention, environment variable names are all uppercase. You can create your own environment variables, or you can use the predefined environment variables below.
These environment variables have a corresponding C shell variable. When either one changes, the value is copied to the other:
Other environment variables include the following:
A string of ex commands similar to those found in
the startup .exrc
file (e.g., set ai).
Used by vi and ex.
The file that holds mail. Used by mail programs. This is not the same as the C shell mail variable, which only checks for new mail.
Undefined by default; once initialized to shell, the two are identical.
The file that holds the cursor-positioning codes for your terminal type. Default is /etc/termcap.