This subsection describes the following:
Variable substitution
Variable modifiers
Predefined shell variables
Formatting for the prompt variable
Sample .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 considered part of the name:
Variable | Description |
---|---|
${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 also can 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. |
${argv[n]} | Individual arguments on command line (positional parameters); 1 |
${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. |
${?name} | Return 1 if name is set, 0 if not. |
$?0 | Return 1 if input filename is known, 0 if not. |
Sort the third through last arguments and save the output in a file whose name is unique to this process:
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, $$, and $?0, the variable substitutions in the preceding section may be followed by one of these modifiers (when braces are used, the modifier goes inside them):
Return the variable's root (the portion before the last dot).
Return the variable's extension.
Return the variable's header (the directory portion).
Return the variable's tail (the portion after the last slash).
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 following 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 |
Unless quoted, the shell expands variables to represent files in the current directory:
% 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:
or by simply turning the variable on:set var=value
set var
In the following list, variables that accept values are shown with the equals sign followed by the type of value they accept; the value then is 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. tcsh automatically sets (and, in some cases, updates) the variables addsuffix, argv, autologout, cwd, dirstack, echo-style, edit, gid, home, loginsh, logout, oid, owd, path, prompt, prompt2, prompt3, shell, shlvl, status, tcsh, term, tty, uid, user, and version. Variables in italics are specific to tcsh.
tcsh provides a list of substitutions that can be used in formatting the prompt. (csh allows only plain-string prompts and the ! history substitution shown in the following list.) The list of available substitutions includes:
Literal %
The present working directory
The present working directory, in ~ notation
# for the superuser, > for others
Previous command's exit status
End boldfacing
The last n (default 1) components of the present working directory; if 0 is specified, replace removed components with /<skipped>
Day of the week (e.g., Mon, Tue)
Number of current history event
Current tty
First component of hostname
Username
Current time, with seconds (12-hour mode)
End standout mode (reverse video)
Current time (12-hour format)
End underlining
Month (e.g., Jan, Feb)
Year (e.g., 99, 00)
Begin boldfacing
Similar to %c, but uses full pathnames instead of ~ notation
Day of month (e.g., 09, 10)
Fully qualified hostname
Current time, with seconds (24-hour format)
Begin standout mode (reverse video)
Current time (24-hour format)
Begin underlining
Month (e.g., 09, 10)
Year (e.g., 1999, 2000)
# 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 noclobber ignoreeof set cdpath=(/usr/lib /usr/spool/uucp) # Now I can type cd macros # instead of cd /usr/lib/macros set history=100 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 exported automatically, 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 predefined environment variables that follow.
The following environment variables have corresponding C-shell variables. When either one changes, the value is copied to the other (italics means the variable is specific to tcsh):
User's group name; same as group.
Home directory; same as home.
Search path for commands; same as path.
Number of nested shell levels; same as shlvl.
Terminal type; same as term.
User's login name; same as user.
Other environment variables, which do not have corresponding shell variables, include the following (italics means the variable is specific to tcsh):
Number of columns on terminal.
Identifies user's display for the X Window System. If set, the shell doesn't set autologout.
Pathname to default editor. See also VISUAL.
Name of machine.
Type of machine. Obsolete; will be removed eventually.
Colon-separated list of directories to search for documentation.
Preferred language. Used for native language support.
The locale, as it affects character handling. Used for native language support.
Number of lines on the screen.
Another name for the USER variable.
Type of machine.
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.
Printable characters not rebound. Used for native language support.
Operating system.
The current directory; the value is copied from cwd.
Machine name of remote host.
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.
The system vendor.
Copyright © 2001 O'Reilly & Associates. All rights reserved.