This appendix briefly summarizes shell features and characteristics. Examples and additional commentary may be found elsewhere in this handbook. See also the csh and tcsh manual pages.
A simple command (designated cmd
below) consists of a command name, possibly followed by one or more arguments. This basic form may be modified or combined with other commands to form more complex command sequences:
cmd
Runcmd
cmd
& Runcmd
in the backgroundcmd
>file
Writecmd
output tofile
(overwritesfile
)cmd
>>file
Writecmd
output tofile
(appends tofile
)cmd
<file
Takecmd
input fromfile
cmd1
;cmd2
Runcmd1
, thencmd2
cmd1
|cmd2
Writecmdl
output to input ofcmd2
(cmd1
;cmd2
) Runcmdl
cmdcmd2
in subshellcmd1 'cmd2'
Usecmd2
to produce arguments forcmd1
The shell reads one or more files from your home directory when it starts up, and possibly one file when it terminates. The files are described in the following table. No error occurs if any given file does not exist.
At startup, a login shell reads, in order, ˜/.cshrc, ˜/.history, ˜/.login, and (for tcsh only) ˜/.cshdirs. A non-login shell reads only ˜/.cshrc at startup.
At termination, a login shell reads ˜/.logout and writes ˜/.history and (for tcsh only) ˜/.cshdirs if the appropriate variables are set. (˜/.history is saved if savehist is set. In tcsh, ˜/.cshdirs is saved if savedirs is set.) A non-login shell simply exits.
To execute commands in ˜/.cshrc that will only apply to interactive shells, place them inside the following construct:
if ($?prompt) then
commands
endif
To execute commands only for tcsh, place them inside this construct:
if ($?tcsh) then
commands
endif
There are two types of variables: shell variables and environment variables. Shell variables are available only to the shell in which they are defined. Environment variables are available to the current shell and also to other processes (including subshells) that are started from the shell. By convention, shell variables are lowercase and environment variables are uppercase.
Some useful shell variables are listed in the table below.
Many environment variables (such as HOME, PATH, SHELL, TERM, and USER) are used like the corresponding shell variables with lowercase names. Other useful environment variables are listed in the table below.
Variable | Description |
| |
| |
| The current machine name (tcsh only). |
| Like USER. |
| Like cwd. |
| Pathname of your preferred full-screen editor. |
You can define shell and environment variables, examine their values, or remove them as indicated below.
Command | Description |
| Define shell variable, no value (just turn it on) |
| Define shell variable with explicit value |
| Define shell variable with value consisting of multiple strings |
| Define environment variable |
| Display all shell variable values |
| Display all environment variable values |
| Remove shell variable |
| Remove environment variable |
A read-only shell variable can be defined using set –r instead of set. Read-only variables cannot be modified or removed.
Shell variables are usually set in ˜/.cshrc. Environment variables are usually set in ˜/.login.
Several characters have special meaning to the shell.
Character(s) | Description |
| Filename pattern matching and expansion |
| Variable reference |
| Pipe |
| Input and output redirection |
| |
| Background execution |
| Command separator |
| Argument separator |
| |
| Filename completion (csh) |
| Subshell execution |
| Command substitution |
Quote characters |
Filename arguments can contain the following special characters in order to do pattern matching or home-directory substitution.
Character(s) | Description |
| Match an arbitrary length sequence of characters |
| Match a single character |
| Match any character named between brackets |
| Match any character not named between brackets (tcsh only) |
| Match filenames not matching |
| String expansion operator |
| Expands to pathname of your home directory |
| Expands to pathname of home directory for user |
Quote characters turn off any special meaning a character has, allowing it be used as an ordinary character.
The shell’s history mechanism allows you to recall previous events (commands) and then repeat them. To enable command history, set the history shell variable in ˜/.cshrc to the number of events that you want the shell to remember. To preserve your history across logins, set the savehist shell variable.
The history command displays your current history:
history Display entire history list historyn
Display lastn
events from history list
History references begin with a !
character. When the shell sees such a reference, it replaces it in the command line, echoes the resulting command, and executes it.
The event specifiers listed below select commands from the history list:
Specifier | Description |
| Repeat previous command |
| Repeat command |
| Repeat |
| Repeat last command beginning with |
| Repeat last command containing |
| Repeat the current command line typed so far |
The word designators listed below extract particular words from an event:
Designator | Description |
| Word 0 (the command name) |
| Word |
| Word 1 |
| Last word |
| Words |
| Words 0 through |
| Words |
| Words 0 through (but not including) last word |
| Words |
| Words 1 through last word, or empty if there are no arguments |
|
Word designators are usually separated from the preceding event specifier by a colon. However, some shortcuts may be used: if a designator begins with *
, ^
, $
, or %
, you can omit the colon; if you apply a designator to the previous command (!!
), you can shorten !!
to !
; and you can designate all arguments, the first argument, or the last argument of the previous command using !*
, ! ^
, or !$
.
To fix or change a word from the previous command, use ^
old
^
new
:
%mroe file1 file2 file3
mroe: Command not found. %^ro^or
more file1 file2 file3
Other modifiers are listed below. They are appended to history references that begin with !
, and are separated from the reference by a colon.
Modifier | Description |
| Root of filename (everything but extension following dot) |
| Extension of filename (suffix following dot) |
| Head of pathname (all but last component) |
| Tail of pathname (last component) |
| Perform substitution, replacing |
| Repeat previous |
| Apply modifier following |
| Print resulting command without executing it |
| Quote words (prevents filename pattern expansion) |
| Like |
| Make first lowercase letter uppercase (tcsh only) |
| Make first uppercase letter lowercase (tcsh only) |
| Apply modifier(s) following |
To move around the file system, you specify a directory name as the argument to a directory-changing command. The directories shown below have special names.
Name | Description |
| |
| Parent of current directory |
| Your home directory |
| Home directory for user |
The cd command changes to a given directory, which becomes your current directory:
cd Move to your home directory cd dir1 Move down one level to dir1 cd dir1/dir2 Move down two levels, through dir1 to dir2 cd .. Move up one level to parent directory cd ../.. Move up two levels to parent of parent cd ../dir3 Move up a level, then back down to dir3 cd / Move to root directory (top of file system) cd ˜dubois Move to home directory of dubois account cd - Move to last location (tcsh only)
The shell maintains a directory stack. Stack entries are numbered, starting from 0. pushd and popd change directory (like cd ), but they also add and remove entries from the stack as shown below:
pushddir
Adddir
to stack and change to it pushd Exchange top two stack entries pushd +n
Rotate stack so entryn
is on top popd Drop top entry and return to previous entry popd +n
Drop entryn
entry from stack (tcsh, some versions of csh)
The dirs command displays or clears the directory stack:
dirs Display stack dirs -l Display stack using long names (no˜
name
abbreviations) dirs -n Display stack, wrapping output (tcsh only) dirs -v Display stack, including entry numbers, one line per entry (tcsh only) dirs -c Clear stack (tcsh only)
In tcsh, the –l, –n, and –v flags can also be used with cd, pushd, and popd, and have the same meaning as for dirs.
In tcsh, typing =
n
at the beginning of a command argument is equivalent to typing the value of stack entry n
. =-
refers to the final entry. Filename completion may be used with arguments that begin with =
n
/
or =-/
.
Aliases allow short names to be given to commands or command sequences. Invoking the alias name is equivalent to invoking the corresponding commands. To create or display alias definitions, or to remove aliases, use the following commands:
aliasname definition
Define aliasname
aliasname
Display definition for aliasname
alias Display all alias definitions unaliasname
Remove aliasname
unalias * Remove all aliases
If a definition contains special characters, it should be quoted.
When you invoke an alias, any arguments are added to the end of the command to which the alias expands. To place arguments at a specific location within the command, use one of the following sequences in the alias definition.
Sequence | Description |
| All arguments |
| First argument |
| Last argument |
| Argument |
To use filename completion, type a prefix of a filename and hit the completion key. If the prefix is unique, the shell types the rest of the filename for you. Otherwise, it types as much as is common to all matches, and then beeps. In tcsh, the completion key is TAB
and filename completion is always active. In csh, the completion key is ESC
; in addition, you must set the filec variable in ˜/.cshrc to turn on filename completion.
To list completion matches for a prefix (e.g., when the prefix is ambiguous), type CTRL-D
.
Words beginning with ˜
are completed as home directory references. In tcsh, words beginning with $
are completed as variable names and words beginning with =
n
are completed as though they began with the directory named by directory stack n
.
Setting the fignore shell variable to a list of filename suffixes causes names ending in those suffixes to be ignored for completion.
tcsh allows programmed completions to be specified using the complete command, as shown below:
completecommand word
/pattern
/list
/suffix
command
is the command to which the completion applies. The argument following the command is a completion rule that specifies how to complete a word or words from the command. Although the delimiter between parts of a completion rule is often a dash, it can be any character.
There may be more than one completion rule. Each rule has the parts listed below.
Part | Description |
| Specifies how to select words from the command. It must be |
| Deter mines to which words the rule applies. |
| The word list from which to choose completions. The available lists are described below. |
| An optional suffix to be added to completed words. If missing, a slash is added to completed directory names and a space otherwise. If present, should be a single character. If the character is the same as the delimiter between parts of the completion rule, no suffix is added to completed words. |
The word
specifier determines how pattern
is interpreted. If word
is p, pattern
indicates the word position or positions to which the rule applies. Otherwise, pattern
is a filename pattern and the rule applies to any word whose leftmost part is matched by the pattern.
The word lists from which completions can be chosen are listed below.
List | Description |
| Alias names |
| Key binding names (command-line editor commands) |
| Command names |
| Directory names |
| Environment variable names |
| Filenames (any type, including directory names) |
| Group names |
| |
| Resource limit names |
| Null list (suppresses completion) |
| Shell variable names |
| Signal names |
| Plain text filenames (actually, any non-directory) |
| Variable names (any type) |
| User names |
| Command names for which completions have been defined |
| Explain; like |
| Like |
| Select completion from words in the given list |
| Select completion from words in the value of the variable |
| Select completion from words in the output of the command |
Job control allows you to suspend or terminate jobs, or move jobs between the foreground and background. Job control commands are shown below. When used, %
j
indicates the job of interest.
Command | Description |
| |
| Interrupt (terminate) foreground job |
| Bring stopped or background job to foreground |
| Move stopped job to background |
| Kill (terminate) stopped or background job |
| Stop background job |
| Suspend current shell (if non-login shell) |
| Display current job list |
The job specifier %
j
has the following forms.
Specifier | Description |
| Current job ( |
| Previous job |
| Job number |
| Job whose command line begins with |
| Job whose command line contains |
The fg and bg commands have the shorthand forms listed below.
Specifier | Description |
| Equivalent to |
| Equivalent to |
| Equivalent to |
| Equivalent to |
If the notify shell variable is set, the shell notifies you immediately when background jobs finish. Otherwise, notification occurs when the next prompt is printed.
Use stty to specify whether or not background jobs can write to your terminal, as shown below:
stty tostop Force background jobs to stop when ready to write to terminal stty -tostop Allow background jobs to write to terminal
tcsh lets you interactively browse up and down through your history list, selecting and editing commands before executing them.
Use CTRL-P
and CTRL-N
(or up arrow and down arrow) to move up and down through your history list. Each recalled command is displayed on the current command line. To execute the command, hit RETURN
. To cancel it, hit CTRL-C
.
To edit commands, tcsh provides a command line editor that uses emacs-like or vi-like key bindings. Select the appropriate set using bindkey -e or bindkey -v. Some of the most useful editing commands are shown in the rest of this section. (Use bindkey with no arguments to see all your key bindings.)
In emacs mode, characters are inserted into the command line unless they are editing commands. You can move the cursor or delete characters using the commands shown below.
Command | Description |
| Delete character to left of cursor |
| Delete character under cursor |
| Delete word |
| Delete word backward |
| Delete from cursor to end of line |
| Delete entire line |
Preceding a command with ESC
n
repeats the command n
times.
To search up through your history list for a command containing a string, type the string and then ESC p
. The matching command is pulled into the edit buffer. To search down, type a string and ESC n
. The string can contain filename pattern characters. To repeat the search, simply type ESC p
or ESC n
.
There are two editing modes with the vi bindings. In insert mode, all characters (with a few exceptions, listed below) are inserted into the command line. In command mode, characters are interpreted as editing commands.
Hit ESC
in insert mode to enter command mode. In command mode, several commands enter insert mode until you type ESC
. If you don’t know which mode you are in, hit ESC
until tcsh beeps, and then you will be in command mode.
Some common commands for both modes are shown below.
Command | Description |
| Move cursor back (left) one character |
| Move cursor forward (right) one character |
| Move cursor to beginning of line |
| Move cursor to end of line |
Command | Description |
| Delete character to left of cursor |
| Delete word backward |
| Delete from beginning of line to cursor |
| Delete from cursor to end of line |
Command | Description |
| Move cursor right one word |
| Move cursor left one word |
| Move cursor to next word ending |
| Like |
| Move cursor to beginning of line (first non-whitespace character) |
| Move cursor to beginning of line |
| Move cursor to end of line |
w, b,
and e
stop at whitespace or punctuation. W, B,
and E
stop at whitespace only.
Command | Description |
| Move cursor to next instance of |
| Move cursor to previous instance of |
| Move cursor up to next instance of |
| Move cursor back to previous instance of |
| Repeat previous |
| Repeat previous |
Command | Description |
| Append new text after cursor until |
| Insert new text before cursor until |
| Append new text after end of line until |
| Insert new text before beginning of line until |
Command | Description |
| Delete character under cursor |
| Delete character to left of cursor |
| Delete from cursor to end of motion command |
| Synonym for |
| Delete word backward |
| Delete from beginning of line to cursor |
| Delete from cursor to end of line |
Command | Description |
| Change characters from cursor to end of motion command |
| Synonym for |
| Replace character under cursor with character |
| Replace multiple characters until |
| Substitute character under cursor with characters typed until |
In vi command mode, you can repeat most commands. Simply type the repeat count before the command, e.g., 3dw
to delete three words.
To search up through your history list in vi command mode, type ?
followed by a string, then RETURN.
To search down, use /
. The command containing the string is retrieved into the edit buffer. The string can contain filename pattern characters. To repeat a search in the same or opposite direction, use n
or N
.
bindkey is used to select, examine, and define key bindings. It has several forms:
bindkey -e Select emacs bindings bindkey -v Select vi bindings bindkey -d Restore default bindings bindkey -u Display bindkey usage message bindkey -l List editing commands and their meanings bindkey List all key bindings bindkeykey
List binding forkey
bindkeykey cmd
Bindkey
to editing commandcmd
bindkey -ckey cmd
Bindkey
to UNIX commandcmd
bindkey -skey str
Bindkey
to stringstr
bindkey -rkey
Remove binding forkey
bindkey -l displays a full list of editing command names along with short descriptions of each. Appendix C, Other Sources of Information, references a document that describes the command meanings in more detail.
The flags listed below modify the interpretation of the key
argument. -k and -b cannot both be used in the same command.
Flag | Description |
| Key binding applies to alternate key map (vi command mode key map) |
| Allows |
| Allows |
| If |
Characters in the key
argument may be ordinary characters, or one of the sequences listed below to specify special characters.
Sequence | Character Represented by Sequence |
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| ASCII character with octal code |
The caret sequences have their special meaning whether quoted or unquoted. The backslash in backslash sequences must be doubled if the sequence is unquoted.
c
for c
not shown above produces a literal c
. CTRL-V CTRL-
X
produces a literal CTRL-
X
.
The same sequences may be used for the cmd
and str
arguments of bindkey -c and bindkey -s, except that sequences beginning with a caret are interpreted literally inside quotes.