Assign the value of the arithmetic expression to variable
or to the nth element of variable if the index n
is specified. With no variable or expression
specified, print the values of all shell variables (same as set).
operators as well as examples are listed under Section 8.5, "Expressions"
earlier in this chapter. Two special forms also are valid:
- @ variable++
Increment variable by 1.
- @ variable--
Decrement variable by 1.
Ignore all text that follows on the same line. # is used in shell
scripts as the comment character and is not really a command.
Used as the first line of a script to
invoke the named shell (with optional arguments). Not supported
in all shells. For example:
Null command. Returns an exit status of 0.
The colon command often is put as the first character of a Bourne-
or Korn-shell script to act as a place-holder to keep a
# (hash) from accidentally becoming the first character.
alias [name [command]]
Assign name as the shorthand name, or alias, for command.
If command is omitted, print the alias for name; if
name also is omitted, print all aliases. Aliases can be defined
on the command line, but more often they are stored in .cshrc
so that they take effect upon logging in.
(See the sample .cshrc file earlier in this chapter.)
Alias definitions can
reference command-line arguments, much like the history list. Use
\!* to refer to all command-line arguments, \!^ for
the first argument, \!\!:2 for the second, \!$ for the last, and so on. An alias
name can be any valid Unix command; however, you lose the
original command's meaning unless you type \name. See also
unalias and the "Special Aliases in tcsh" section.
Set the size for xterm windows under the X Window System:
alias R 'set noglob; eval `resize` unset noglob'
Show aliases that contain the string ls:
alias | grep ls
Run nroff on all command-line arguments:
alias ms 'nroff -ms \!*'
Copy the file that is named as the first argument:
alias back 'cp \!^ \!^.old'
Use the regular ls, not its alias:
Print totals of used and free memory.
Put the current job or the jobIDs in the background.
To place a time-consuming process in the background, you might begin
4% nroff -ms report Ctrl-Z
and then issue any one of the following:
5% bg % Current job
5% bg %1 Job number 1
5% bg %nr Match initial string nroff
5% % &
bindkey [options] [key] [command]
Display all key bindings, or bind a key to a command.
List standard and alternate key bindings.
- -b key
Expect key to be one of the following: a control character
(in hat notation -- e.g., ^B -- or C notation -- e.g., C-B); a metacharacter
(e.g., M-B); a function key (e.g., F-string); or
an extended prefix key (e.g., X-B).
- -c command
Interpret command as a shell, not editor, command.
- -d key
Bind key to its original binding.
Bind to standard Emacs bindings.
- -k key
Expect key to refer to an arrow (left, right,
up, or down).
List and describe all editor commands.
- -r key
Completely unbind key.
Interpret command as a literal string
and treat as terminal input.
Print usage message.
Bind to standard vi bindings.
Resume execution following the end command of the nearest
enclosing while or foreach.
Break from a switch; continue execution after the endsw.
Print all built-in shell commands.
tcsh only. Same as logout.
case pattern :
Identify a pattern in a switch.
Change working directory to dir. Default is
user's home directory. If dir is a relative pathname but
is not in the current
directory, the cdpath variable is searched.
See the sample .cshrc file earlier in this chapter.
tcsh includes some options
Change to previous directory.
Explicitly expand ~ notation.
Wrap entries before end-of-line; implies
Print directory stack.
Print entries one per line; implies
Same as cd. Useful if you are redefining cd.
complete [string [word/pattern/list[:select]/[suffix]]]
List all completions, or, if specified, all completions for
string (which may be a pattern). Further options
Options for word
Complete current word only and without referring to
Complete current word only, referring to pattern.
Complete previous word.
Complete word before previous word.
Expect pattern to be a range of numbers. Perform
completion within that range.
Options for list
Various lists of strings can be searched
for possible completions. Some list options include:
Members of the list string
Words from variable
Output from command
External (not built-in) commands
Directories whose names begin with string
Filenames that begin with string
Text files whose names begin with string
Like n but
prints select as an explanation with the editor
select should be a glob pattern. Completions are
limited to words that match this pattern. suffix is
appended to all completions.
Resume execution of nearest enclosing while or foreach.
Label the default case (typically last) in a switch.
Print the directory stack, showing the current directory first.
See also popd and pushd. All options except -l,
Clear the directory stack.
Expand the home directory symbol (~) to the
actual directory name.
Print one directory per line.
- -L file
Re-create stack from file, which should have been created by
dirs -S file.
- -S file
Print to file a series of pushd and popd commands, that can be invoked to replicate the
echo [-n] string
Write string to standard output; if -n is specified, the
output is not terminated by a newline. Unlike the Unix version
(/bin/echo) and the Bourne-shell version, the C shell's
echo doesn't support escape characters. See also echo in
Chapter 3, "Linux Commands", and Chapter 7, "bash: The Bourne-Again Shell".
echotc [options] arguments
Display terminal capabilities, or move cursor on screen, depending on the
Return empty string, not error, if capability
Display verbose messages.
Display current baud.
Display current column.
- cm column row
Move cursor to specified coordinates.
Move cursor to home position.
Print number of lines per screen.
Does this terminal have meta capacity (usually the Alt key)?
Does this terminal have tab capacity?
Reserved word for interior of if ... endif statement.
Reserved word that ends a foreach or switch statement.
Reserved word that ends an if statement.
Reserved word that ends a switch statement.
Typically, eval is used in shell scripts, and args is a
line of code that may contain shell variables. eval forces variable
expansion to happen first and then runs the resulting command. This
"double scanning" is useful any time shell variables contain
input/output redirection symbols, aliases, or other shell variables.
(For example, redirection normally happens before variable expansion,
so a variable containing redirection symbols must be expanded first
using eval; otherwise, the redirection symbols remain
The following line can be placed in the .login file to set up
set noglob eval `tset -s xterm` unset noglob
The following commands show the effect of eval:
% set b='$a'
% set a=hello
% echo $b Read the command line once
% eval echo $b Read the command line twice
Another example of eval can be found under alias.
Execute command in place of current shell.
This terminates the current shell, rather than create
a new process under it.
Exit a shell script with the status given by expr. A status of zero
means success; nonzero means failure. If expr is not specified,
the exit value is that of the status variable.
exit can be issued at the command line to close a window (log out).
Bring the current job or the jobIDs to the foreground.
jobID can be %job-number.
If you suspend a vi editing session (by pressing Ctrl-Z),
you might resume vi using any of these commands:
% fg %
% fg %vi Match initial string
filetest -op files
Apply op file-test operator
to files. Print results in a list.
See Section 18.104.22.168, "File inquiry operators" for the list of file-test operators.
foreach name (wordlist)
Assign variable name to each value in wordlist and
execute commands between foreach and end.
You can use foreach as a multiline command
issued at the C-shell prompt (first of the following examples),
or you can use it in a shell script (second example).
Rename all files that begin with a capital letter:
% foreach i ([A-Z]*)
? mv $i $i.new
Check whether each command-line argument is an option or not:
foreach arg ($argv)
# does it begin with - ?
if ("$arg" =~ -*) then
echo "Argument is an option"
echo "Argument is a filename"
Do filename, variable, and history substitutions on wordlist.
No \ escapes are recognized in its expansion,
and words are delimited by null characters.
glob typically is used in shell scripts to hardcode a value
so that it remains the same for the rest of the script.
Skip to a line whose first nonblank character is string
followed by a colon and continue execution below that line.
On the goto
line, string can be a variable or filename pattern,
but the label branched to must be a literal, expanded value
and must not occur within a foreach or while.
Display statistics that show the hash table's level of success at locating
commands via the path variable.
Display the list of history events. (History syntax is discussed
earlier, in "Command History.")
tcsh only. Clear history list.
Print history list without event numbers.
Print in reverse order; show oldest commands last.
Display only the last n history commands, instead
of the number set by the history shell variable.
- -L file
tcsh only. Load series of pushd and popd commands from file
in order to re-create a saved stack.
- -M file
tcsh only. Merge the current directory stack and the stack saved in file. Save both, sorted by time, in file, as a series of pushd
and popd commands.
- -S file
tcsh only. Print to file a series of pushd and popd commands that can be invoked to replicate the
To save and execute the last five commands:
history -h 5 > do_it
tcsh only. Start command but make it exit when sent a hangup signal,
which is sent when shell exits. By default, configure shell script
to exit on hangup signal.
Begin a conditional statement. The simple format is:
if (expr) cmd
There are three other possible formats, shown side-by-side:
if (expr) then if (expr) then if (expr) then
cmds cmds1 cmds1
endif else else if (expr) then
In the simplest form, execute cmd if expr is true;
otherwise do nothing (redirection still occurs; this is a bug). In
the other forms, execute one or more commands.
If expr is true, continue with the commands after then;
if expr is false, branch to the commands after else
(or branch to after the else if and continue checking).
For more examples, see Section 8.5, "Expressions" earlier in this chapter,
shift, or while.
Take a default action if no command-line arguments are given:
if ($#argv == 0) then
echo "No filename given. Sending to Report."
set outfile = Report
set outfile = $argv
List all running or stopped jobs; -l includes process IDs.
For example, you can check whether a long compilation or text format
is still running. Also useful before logging out.
kill [options] ID
Terminate each specified process ID or job ID.
You must own the process or be a privileged user.
This built-in is similar to /bin/kill
described in Chapter 3, "Linux Commands" but also allows symbolic job names.
Stubborn processes can be killed using signal 9.
List the signal names. (Used by itself.)
The signal number or name, without the SIG prefix (e.g., HUP, not SIGHUP).
The command kill -l prints a list of
the available signal names. The list varies by system architecture; for
a PC-based system, it looks like this:
The signals and their numbers are defined in
/usr/include/asm/signal.h; look in that file to
find the signals that apply to your system.
% kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2
PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG
XCPU XFSZ VTALRM PROF WINCH POLL PWR UNUSED
If you've issued the following command:
44% nroff -ms report &
you can terminate it in any of the following ways:
45% kill 19536 Process ID
45% kill % Current job
45% kill %1 Job number 1
45% kill %nr Initial string
45% kill %?report Matching string
limit [-h] [resource [limit]]
Display limits or set a limit on resources
used by the current process and by each
process it creates. If no limit is given, the current limit is printed for
resource. If resource also is omitted, all limits are printed.
By default, the current limits are shown or set; with -h,
hard limits are used. A hard limit imposes an absolute limit that can't
be exceeded. Only a privileged user may raise it.
See also unlimit.
Use hard, not current, limits.
Maximum number of seconds the CPU can spend;
can be abbreviated as cpu.
Maximum size of any one file.
Maximum size of data (including stack).
Maximum size of stack.
Maximum size of a core dump file.
A number followed by an optional character (a unit specifier).
||nh (for n hours)
||nm (for n minutes)
||mm:ss (minutes and seconds)
||nk (for n kilobytes, the default)
||nm (for n megabytes)
Consult the watch variable for list of users being watched. Print list of those who are presently logged in. If - is entered as an option, reset environment as if user had logged in with new group.
Replace user's login shell with /bin/login.
-p is used to preserve environment variables.
Terminate the login shell.
ls-F [options] [files]
Faster alternative to ls -F. If given any options, invokes
newgrp [-] [group]
Change user's group ID to specified group ID, or, if none is specified, to original group ID. If - is entered as an option, reset environment as if user had
logged in with new group. Must have been compiled into the shell; see the version
nice [+n] command
Change the execution priority for command, or, if
none is given, change priority for the current shell.
(See also nice in Chapter 3, "Linux Commands".)
The priority range is -20 to 20, with a default of 4.
The range seems backward: -20 gives the highest priority
(fastest execution); 20 gives the lowest. Only a privileged user may specify
a negative number.
Add n to the priority value (lower job priority).
Subtract n from the priority value (raise job priority).
Privileged users only.
"No hangup signals."
Do not terminate command after terminal line is
closed (i.e., when you hang up from a phone or log out).
Use without command in shell scripts to keep script from
(See also nohup in Chapter 3, "Linux Commands".)
Report immediately when a background job finishes
(instead of waiting for you to exit a long editing session,
for example). If no jobID is given, the current background
job is assumed.
"On interrupt." Used in shell scripts to handle interrupt signals
(similar to bash's trap 2 and trap "" 2 commands).
The first form is like a goto label. The script will
branch to label: if it catches an interrupt signal (e.g., Ctrl-C).
The second form lets the script ignore interrupts. This is useful
at the beginning of a script or before any code segment that needs
to run unhindered (e.g., when moving files).
The third form restores interrupt handling that was previously disabled
with onintr -.
onintr cleanup Go to "cleanup" on interrupt
. Shell script commands
cleanup: Label for interrupts
onintr - Ignore additional interrupts
rm -f $tmpfiles Remove any files created
exit 2 Exit with an error status
Remove the current entry from the directory stack,
or remove the nth entry from the stack
and print the stack that remains. The current
entry has number 0 and appears on the left.
See also dirs and pushd.
Specify nth entry.
Expand ~ notation.
Wrap long lines.
Override the pushdsilent shell variable,
which otherwise prevents the printing of the final stack.
Print precisely one directory per line.
Print all (or one specified) environment variables and their
The first form changes the working directory to name and adds it
to the directory stack. The second form rotates the nth entry to the
beginning, making it the working directory. (Entry numbers begin at 0.)
With no arguments, pushd switches the first two entries
and changes to the new current directory. The +n, -l,
-n, and -v options behave the same as in popd.
See also dirs and popd.
% pushd /etc Add /etc to directory stack
/etc /home/bob /usr
% pushd +2 Switch to third directory
/usr /etc /home/bob
% pushd Switch top two directories
/etc /usr /home/bob
% popd Discard current entry; go to next
Recompute the internal hash table for the PATH variable.
Use rehash whenever a new command is created during the current session.
This allows the PATH variable to locate and execute the command.
(If the new command resides in a directory not listed in PATH,
add this directory to PATH before rehashing.)
See also unhash.
repeat n command
Execute n instances of command.
Print three copies of memo:
% repeat 3 pr memo | lp
Read 10 lines from the terminal and store in item_list:
% repeat 10 line > item_list
Append 50 boilerplate files to report:
% repeat 50 cat template >> report
sched time command
Without options, print all scheduled events. The second form schedules an
time should be specified in hh:mm form (e.g., 13:00).
Schedule event to take place hh:mm from now.
Remove nth item from schedule.
set [option] variable[n]=value
Set variable to value, or if multiple
values are specified, set the
variable to the list of words in the value list. If an index n
is specified, set the nth word in the variable to value.
(The variable must already contain at least that number of words.)
With no arguments, display the names and values of all set variables.
See also "Predefined Shell Variables" earlier in this chapter.
tcsh only. List only read-only variables, or set specified variable to read-only.
% set list=(yes no maybe) Assign a wordlist
% set list=maybe Assign an item in existing wordlist
% set quote="Make my day" Assign a variable
% set x=5 y=10 history=100 Assign several variables
% set blank Assign a null value to blank
setenv [name [value]]
Assign a value to an environment variable name.
By convention, name is uppercase.
value can be a single word or a quoted string.
If no value is given, the null value is assigned.
With no arguments, display the names and values of all
environment variables. setenv is not necessary for the
PATH variable, which is automatically exported from path.
settc capability value
tcsh only. Set terminal capability to
setty [options] [+|-mode]
Do not allow shell to change specified tty modes. By default, act
on the execute set.
Without arguments, list all modes in specified set that are on. Otherwise, set specified mode to on.
Without arguments, list all modes in specified set that are off. Otherwise, set specified mode to on.
List all modes in specified set.
Act on the edit set of modes (used when editing commands).
Act on the quote set of modes (used when entering characters verbatim).
Act on the execute set of modes (default) (used when executing examples).
If variable is given, shift the words in a wordlist variable (i.e., name becomes name). With no argument,
shift the positional parameters (command-line arguments) (i.e.,
$2 becomes $1). shift is typically used in a while loop.
See additional example under while.
while ($#argv) While there are arguments
if (-f $argv)
wc -l $argv
echo "$argv is not a regular file"
shift Get the next argument
source [-h] script [args]
Read and execute commands from a C-shell script.
With -h, the commands are
added to the history list but aren't executed. For tcsh only,
arguments can be passed to the script and are put in argv.
Suspend the current background jobs or the
background jobs specified by jobIDs; this is
the complement of Ctrl-Z or suspend.
Suspend the current foreground job; same as Ctrl-Z.
Often used to stop an su command.
Process commands depending on the value of a variable.
When you need to handle more than three choices, switch
is a useful alternative to an if-then-else statement.
If the string variable matches pattern1,
the first set of commands is executed; if string
matches pattern2, the second set of commands is executed;
and so on. If no patterns match, execute commands under the
string can be specified using command substitution,
variable substitution, or filename expansion.
Patterns can be specified using the pattern-matching symbols *,
?, and . breaksw is used to exit the switch.
If breaksw is omitted (which is rarely done),
the switch continues to execute another set of commands until
it reaches a breaksw or endsw.
Following is the general syntax of switch, side-by-side with an example
that processes the first command-line argument:
switch (string) switch ($argv)
case pattern1: case -[nN]:
commands nroff $file | lp
case pattern2: case -[Pp]:
commands pr $file | lp
case pattern3: case -[Mm]:
commands more $file
. case -[Ss]:
. sort $file
commands echo "Error -- no such option"
tcsh only. Print all terminal capabilities and their values.
Execute a command and show how much time it uses.
With no argument, time can be used in a shell script to time the script.
Display file creation mask or set file creation mask to octal
nnn. The file creation mask determines which permission bits
are turned off. With no nnn, print the current mask.
Remove all aliases whose names match
pattern from the alias list. See
alias for more information.
Remove completions (specified by complete) whose names
Remove internal hash table. The shell stops using hashed values
and searches the path directories to locate a
command. See also rehash.
unlimit [-h] [resource]
Remove the allocation limits on resource. If resource is not
specified, remove limits for all resources. See limit for more
information. With -h, remove hard
limits. This command can be run only by a privileged user.
Remove one or more variables. Variable names may be specified
as a pattern, using filename metacharacters. Does
not remove read-only variables. See set.
Remove an environment variable. Filename matching is not valid.
Pause in execution until all child processes complete,
or until an interrupt signal is received.
tcsh only. Same as log.
Must have been compiled into the shell; see the version shell variable.
Display all aliases, built-ins, and executables named command.
tcsh only. Report which version of command will be executed.
Same as the executable which, but faster, and checks tcsh
As long as expression is true
(evaluates to nonzero),
between while and end. break and continue can
be used to terminate or continue the loop.
set user = (alice bob carol ted)
while ($argv != $user) Cycle through each user, checking for a match
shift user If we cycled through with no match...
if ($#user == 0) then
echo "$argv is not on the list of users"