This subsection describes the following:
Variable substitution
Variable modifiers
Predefined shell variables
Formatting for the prompt variable
Sample .tcshrc 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 ≤ 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 not. |
$$ |
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 .tcshrc 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):
The following table shows the effect of pathname modifiers if the aa variable is set as follows:
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:
set var=value
or by simply turning the variable 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, and 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, command, 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.
Variable |
Description |
---|---|
Append / to directories and a space to files during tab completion to indicate a precise match. |
|
Set value to be used instead of the local username for Kerberos authentication with the autologout locking feature. |
|
Display all times in 12-hour format. |
|
List of arguments passed to current command; default is ( ). |
|
Check spelling before attempting to complete commands. |
|
Expand history (such as ! references) during command completion. |
|
Print possible completions when correct one is ambiguous. If ambiguous is specified, print possible completions only when completion adds no new characters. |
|
Log out after logout-minutes of idle time. Lock the terminal after locking-minutes of idle time, requiring a password before continuing. Not used if the DISPLAY environment variable is set. |
|
Always allow backslashes to quote \, ', and ". |
|
Use tcsh.${catalog} as the filename of the message catalog. The default is tcsh. |
|
List of alternate directories to search when locating arguments for cd, popd, or pushd. |
|
Turn on color for ls-F, ls, or both. Setting to nothing is equivalent to setting for both. |
|
Enable color escape sequence for Native Language System (NLS) support and display NLS messages in color. |
|
If set, hold the command passed to the shell with the -c option. |
|
When enhance, ignore case in completion, treat ., -, and _ as word separators, and consider _ and - to be the same. |
|
If set to a list of commands, continue those commands instead of starting new ones. |
|
Like continue, but execute the following: echo `pwd` $argv > ~/.cmd_pause; %cmd |
|
When cmd, spellcheck commands. When complete, complete commands. When all, spellcheck whole command line. |
|
Full pathname of current directory. |
|
When set, the pushd command extracts the desired directory and puts it at the top of the stack instead of rotating the stack. |
|
History file consulted by dirs -S and dirs -L. Default is ~/.cshdirs. |
|
Directory stack, in array format. dirstack[0] is always equivalent to cwd. The other elements can be artificially changed. |
|
Enable use of multibyte code; for use with Kanji. See the tcsh manpage for details. |
|
Make sure that each directory exists only once in the stack. |
|
Redisplay each command line before execution; same as csh -x command. |
|
Don't echo a newline with -n option (bsd), parse escaped characters (sysv), do both, or do neither. |
|
Enable command-line editor. Set by default for interactive shells. |
|
For use with prompt variable. Use ... to represent skipped directories. |
|
List of filename suffixes to ignore during filename completion. |
|
User's group ID. |
|
User's group name. |
|
A two-character string that sets the characters to use in history substitution and quick substitution (default is !^). |
|
Maintain a record only of unique history events (all), do not enter a new event when it is the same as the previous one (prev), or remove an old event that is the same as the new one (erase). |
|
History file consulted by history -S and history -L. Default is ~/.history. |
|
Do not expand history lines when recalling them. |
|
The first word indicates the number of commands to save in the history list. The second indicates the format with which to display that list (see Section 8.4.4 for possible formats). |
|
Home directory of user, initialized from HOME. The ~ character is shorthand for this value. |
|
Ignore an end-of-file (EOF) from terminals; prevents accidental logout. |
|
If directory name is entered as a command, cd to that directory. Can be set to verbose to echo the cd to standard output. |
|
Control editor's mode. |
|
Enter only unique strings in the kill ring (all), do not enter new string when it is the same as the current killed string (prev), or erase from the kill ring an old string that is the same as the current string (erase). |
|
Set the number of killed strings to keep in memory to num. The default is 30. If unset or set to a number less than 2, keep only the most recent killed string. |
|
One or more of the x, a, or A options for the ls-F built-in command. Second word can be set to path for ls command. |
|
When a job is suspended, list all jobs (in long format, if specified). |
|
In ls -F command, include type of file to which links point. |
|
Do not allow list-choices to print more than num choices before prompting. |
|
Do not allow list-choices to print more than num rows of choices before prompting. |
|
Set if shell is a login shell. |
|
Indicates status of an imminent logout (normal, automatic, or hangup). |
|
One or more files checked for new mail every 5 minutes or (if n is supplied) every n seconds. |
|
Specifies circumstances under which completion should beep: never, if no match exists, if multiple matches exist, or if multiple matches exist and one is exact. If unset, ambiguous is used. |
|
Disable beeping. |
|
Don't redirect output to an existing file; prevents accidental destruction of files. |
|
Don't print "DING!" in prompt time specifiers when the hour changes. |
|
Turn off filename expansion; useful in shell scripts. |
|
Disable Kanji (if supported). |
|
Treat filename metacharacters as literal characters if no match exists (e.g., vi ch* creates new file ch* instead of printing "No match"). |
|
Do not stat directory-list during completion. |
|
Declare job completions when they occur. |
|
Old working directory. |
|
List of pathnames in which to search for commands to execute. Initialized from PATH; the default is . /usr/ucb /usr/bin. |
|
Print all nonzero exit values. |
|
String that prompts for interactive input; default is %. See Section 8.4.4 later in this chapter for formatting information. |
|
prompt2='str' |
String that prompts for interactive input in foreach and while loops and continued lines (those with escaped newlines). See Section 8.4.4 for formatting information. |
prompt3='str' |
String that prompts for interactive input in automatic spelling correction. See Section 8.4.4 for formatting information. |
Use the two characters specified as cc with the %# prompt sequence to indicate normal users and the superuser, respectively. |
|
Do not print directory stack when pushd and popd are invoked. |
|
Change to home directory when pushd is invoked without arguments. |
|
Consider completion to be concluded on first exact match. |
|
When command completion is invoked, print only executable files. |
|
Prompt before executing the command rm *. |
|
The string to print on the right side of the screen while the prompt is displayed on the left. Specify as for prompt. |
|
Execute dirs -S before exiting. |
|
Execute history -S before exiting. Save no more than max lines of history. If specified, merge those lines with previous history saves, and sort by time. |
|
Format for sched's printing of events. See Section 8.4.4 for formatting information. |
|
shell=file |
Pathname of the shell program. |
Number of nested shells. |
|
Exit status of last command. Built-in commands return 0 (success) or 1 (failure). |
|
Specify manner in which to deal with symbolic links. Expand them to real directory name in cwd (chase), treat them as real directories (ignore), or expand arguments that resemble pathnames (expand). |
|
Version of tcsh. |
|
Terminal type. |
|
If command execution takes more than n CPU seconds, report user time, system time, elapsed time, and CPU percentage. Supply optional %c flags to show other data. |
|
Number of minutes between executions of periodic alias. |
|
Name of tty, if applicable. |
|
User ID. |
|
Username. |
|
Display a command after history substitution; same as tcsh -v. |
|
Shell's version and additional information, including options set at compile time. |
|
Flash screen instead of beeping. |
|
Watch for user logging in at terminal, where terminal can be a tty name or any. Check every n minutes, or 10 by default. |
|
Specify information to be printed by watch. |
|
List of all nonalphanumeric characters that may be part of a word. Default is *?_-.[ ]~=. |
tcsh provides a list of substitutions that can be used in formatting the prompt. The list of available substitutions includes:
# 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
tcsh maintains a set of environment variables, which are distinct from shell variables and aren't really part of the shell. Shell variables are meaningful only within the current shell, but environment variables are exported automatically, making them available globally. For example, 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 tcsh shell variables. When either one changes, the value is copied to the other.
Other environment variables, which do not have corresponding shell variables, include the following:
Copyright © 2003 O'Reilly & Associates. All rights reserved.