This section 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 i of var. i can be a single number, a range m–n, a range -n (missing m implies 1), a range m- (missing n implies all remaining words), or * (select all words). i can also be a variable that expands to one of these values. |
${#var} | The number of words in var. |
${#argv} | The number of arguments. |
$0 | Name of the program. (Usually not set in interactive shells.) |
${argv[n]} | Individual arguments on command line (positional parameters). n = 1–9. |
${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 previous variable substitutions may be followed by one of the following modifiers. When braces are used, the modifier goes inside them.
This table shows the use of pathname modifiers on the following variable:
set 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:
set var=value
or by simply turning them on:
set var
In the following table, variables that accept values are shown with the equals 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=(/var/mail/tom) if ($?prompt) then # Settings for interactive use set echo set filec set noclobber ignoreeof set cdpath=(/usr/lib /var/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:
setenv VAR value
By convention, environment variable names are all uppercase. You can create your own environment variables, or you can use the following predefined environment variables.
These environment variables have a corresponding C shell variable:
Other environment variables include the following:
Copyright © 2003 O'Reilly & Associates. All rights reserved.