Appendix B. csh and tcsh Quick Reference

In This Appendix:

  • Command Structure

  • Startup and Shutdown Files

  • Variables

  • Special Characters

  • Command History

  • Moving Around the File System

  • Aliases

  • Filename Completion

  • Programmed Completion

  • Job Control

  • Command Editing in tcsh

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.

Command Structure

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             Run cmd
cmd &           Run cmd in the background
cmd > file      Write cmd output to file (overwrites file)
cmd >> file     Write cmd output to file (appends to file)
cmd < file      Take cmd input from file
cmd1 ; cmd2     Run cmd1, then cmd2
cmd1 | cmd2     Write cmdl output to input of cmd2
(cmd1 ; cmd2)   Run cmdl cmd cmd2 in subshell
cmd1 'cmd2'     Use cmd2 to produce arguments for cmd1

Star tup and Shutdown Files

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.

File

Description

˜/.cshrc

Read at startup

˜/.tcshrc

Read instead of ˜/.cshrc if it exists (tcsh only—if you’re using tcsh, read references to ˜/.cshrc as "˜/.tcshrc if it exists, ˜/.cshrc otherwise.”)

˜/.login

Read by login shells at startup

˜/.history

Read by login shells at startup to initialize the history list

˜/.cshdirs

Read by login shells at startup to initialize the directory stack (tcsh only)

˜/.logout

Read by login shells at termination

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

Variables

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.

Variable

Description

autologout

If set, automatic logout occurs after you have been inactive for an hour. If set to a number, logout occurs after that many minutes of inactivity. Some shells set autologout by default at login time. To disable, unset autologout in ˜/.cshrc.

cdpath

A list of directories in which the shell looks for directories specified as arguments to cd or pushd.

correct

If set to cmd, turns on spelling correction for command names. If set to all, turns on spelling correction for the entire command line (tcsh only).

cwd

Set by the shell to your current working directory when your directory changes.

fignore

A list of filename suffixes. Filenames ending with any of the suffixes are ignored for filename completion.

filec

In csh, turns on filename completion. (Unnecessary for tcsh.)

history

If set, specifies the number of commands to save in the history list.

home

The pathname to your home directory.

ignoreeof

If set, CTRL-D does not terminate the shell. You need to explicitly type exit or logout.

mail

Pathname of file to check periodically to see if new mail has arrived.

notify

If set, the shell notifies you immediately when background jobs finish. Otherwise it waits until just before the next prompt.

path

A list of directories in which the shell looks for commands.

prompt

Your prompt string. Set only by interactive shells. Can be tested to distinguish interactive from non-interactive shells.

rmstar

If set, tcsh asks “if you really mean it” when it sees * given as an argument to the rm command (tcsh only).

savedirs

If set, login shells save the directory stack in ˜/.cshdirs when you log out. The stack is read back in when you log in again (tcsh only).

savehist

If set, login shells save the history list in ˜/.history when you log out. The history list is read back in when you log in again.

shell

The pathname of your login shell.

tcsh

Set to tcsh version number (tcsh only; can be tested to distinguish tcsh from csh).

term

Your terminal type.

user

Your user (login) name.

version

Shell version and configuration information (tcsh only).

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

DISPLAY

Display specification used by X Window System.

EDITOR

Pathname of your preferred editor.

HOST

The current machine name (tcsh only).

LOGNAME

Like USER.

PWD

Like cwd.

VISUAL

Pathname of your preferred full-screen editor.

Defining, Removing, and Examining Variables

You can define shell and environment variables, examine their values, or remove them as indicated below.

Command

Description

set var

Define shell variable, no value (just turn it on)

set var = value

Define shell variable with explicit value

set var = (str1 str2 str3)

Define shell variable with value consisting of multiple strings

setenv VAR value

Define environment variable

set

Display all shell variable values

setenv

Display all environment variable values

unset var

Remove shell variable

unsetenv VAR

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.

Using Variables

To use a variable, refer to it as $name or ${name}. Modifiers can be applied to a variable reference (as $name:m or ${name:m}) to extract part of the variable’s value.

Modifier

Description

r

Root of value (everything but extension following dot)

e

Extension of value (suffix following dot)

h

Head of value (all but last pathname component)

t

Tail of value (last pathname component)

Special Characters

Several characters have special meaning to the shell.

Character(s)

Description

* ? [ ] ^ { } ˜

Filename pattern matching and expansion

$

Variable reference

|

Pipe

< >

Input and output redirection

! ^

History reference and quick substitution

&

Background execution

;

Command separator

SPACE

Argument separator

TAB

Filename completion (tcsh)

ESC

Filename completion (csh)

()

Subshell execution

''

Command substitution

' "

Quote characters

Special Characters in Filenames

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)

^pattern

Match filenames not matching pattern (tcsh only)

{}

String expansion operator

˜

Expands to pathname of your home directory

˜name

Expands to pathname of home directory for user name

Quote Characters

Quote characters turn off any special meaning a character has, allowing it be used as an ordinary character.

Character(s)

Description

Quote the following character.

''

Quote the characters between single quotes. !event is still evaluated as a history substitution.

""

Quote the characters between double quotes. !event, $var, and 'cmd' are still evaluated as history, variable, and command substitutions.

Command History

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
history n        Display last n events from history list

History Event Specifiers

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

!n

Repeat command n

!-n

Repeat n-th-to-last command

!str

Repeat last command beginning with str

!?str?

Repeat last command containing str

!#

Repeat the current command line typed so far

Word Designators

The word designators listed below extract particular words from an event:

Designator

Description

0

Word 0 (the command name)

n

Word n (argument n)

^

Word 1

$

Last word

m-n

Words m through n

-n

Words 0 through n

m-

Words m through (but not including) last word

-

Words 0 through (but not including) last word

m*

Words m through last word

*

Words 1 through last word, or empty if there are no arguments

%

Following a !?str? event specifier, the word matched by str

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 !$.

History Modifiers

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

r

Root of filename (everything but extension following dot)

e

Extension of filename (suffix following dot)

h

Head of pathname (all but last component)

t

Tail of pathname (last component)

s/old/new/

Perform substitution, replacing old with new

&

Repeat previous s substitution

g

Apply modifier following g globally to each word

p

Print resulting command without executing it

q

Quote words (prevents filename pattern expansion)

X

Like q but break into words at whitespace

u

Make first lowercase letter uppercase (tcsh only)

1

Make first uppercase letter lowercase (tcsh only)

a

Apply modifier(s) following a as many times as possible to a word. If used with g, a is applied to all words. (tcsh only)

Moving Around the File System

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

.

Current directory

..

Parent of current directory

˜

Your home directory

˜name

Home directory for user name

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:

pushd dir        Add dir to stack and change to it
pushd            Exchange top two stack entries
pushd +n         Rotate stack so entry n is on top
popd             Drop top entry and return to previous entry
popd +n          Drop entry n 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

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:

alias name definition     Define alias name
alias name                Display definition for alias name
alias                     Display all alias definitions
unalias name              Remove alias name
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

!:n

Argument n

Filename Completion

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.

Programmed Completion

tcsh allows programmed completions to be specified using the complete command, as shown below:

complete command 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

word

Specifies how to select words from the command. It must be p, c, n, C, or N (described below).

pattern

Deter mines to which words the rule applies.

list

The word list from which to choose completions. The available lists are described below.

suffix

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.

word

Description

p

Position-based completion. pattern is a number or range of numbers specifying to which words the rule applies:

n         Word n
m-n       Words m through n
m*        Words m through last word
*         All words

c

Complete the current word (the word matched by pattern ). The part matched by the pattern is not considered part of the prefix to be completed.

n

Complete the next word after the word matched by pattern.

C

Complete the current word. Unlike c, the entire word is used as the prefix to be completed.

N

Complete the word occurring two words after the word matched by pattern.

The word lists from which completions can be chosen are listed below.

List

Description

a

Alias names

b

Key binding names (command-line editor commands)

c

Command names

d

Directory names

e

Environment variable names

f

Filenames (any type, including directory names)

g

Group names

j

Jobs names

l

Resource limit names

n

Null list (suppresses completion)

s

Shell variable names

S

Signal names

t

Plain text filenames (actually, any non-directory)

v

Variable names (any type)

u

User names

X

Command names for which completions have been defined

x

Explain; like n, but print a message when you type CTRL-D

C, D, F, T

Like c, d, f, t, but select completion from a given directory

(list)

Select completion from words in the given list

$variable

Select completion from words in the value of the variable

'command'

Select completion from words in the output of the command

Job Control

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

CTRL-Z

Stop the foreground job

CTRL-C

Interrupt (terminate) foreground job

fg %j

Bring stopped or background job to foreground

bg %j

Move stopped job to background

kill %j

Kill (terminate) stopped or background job

stop %j

Stop background job

suspend

Suspend current shell (if non-login shell)

jobs

Display current job list

The job specifier %j has the following forms.

Specifier

Description

%

Current job (%+ and %% are synonyms for %)

%-

Previous job

%n

Job number n

%str

Job whose command line begins with str

%?str

Job whose command line contains str (if shell says No match, use %?str)

The fg and bg commands have the shorthand forms listed below.

Specifier

Description

%j

Equivalent to fg %j

fg or %

Equivalent to fg %

%j&

Equivalent to bg %j

bg or %&

Equivalent to bg %

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

Command Editing in tcsh

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.)

emacs Mode Editing Commands

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.

Table B-1. Cursor motion commands

Command

Description

CTRL-B

Move cursor back (left) one character

CTRL-F

Move cursor forward (right) one character

ESC b

Move cursor back one word

ESC f

Move cursor forward one word

CTRL-A

Move cursor to beginning of line

CTRL-E

Move cursor to end of line

Table B-2. Text deletion commands

Command

Description

DEL or CTRL-H

Delete character to left of cursor

CTRL-D

Delete character under cursor

ESC d

Delete word

ESC DEL or ESC CTRL-H

Delete word backward

CTRL-K

Delete from cursor to end of line

CTRL-U

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.

vi Mode Editing Commands

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.

Table B-3. Cursor motion commands (insert mode)

Command

Description

CTRL-B

Move cursor back (left) one character

CTRL-F

Move cursor forward (right) one character

CTRL-A

Move cursor to beginning of line

CTRL-E

Move cursor to end of line

Table B-4. Text deletion commands (insert mode)

Command

Description

DEL or CTRL-H

Delete character to left of cursor

CTRL-W

Delete word backward

CTRL-U

Delete from beginning of line to cursor

CTRL-K

Delete from cursor to end of line

Table B-5. Cursor motion commands (command mode)

Command

Description

w

Move cursor right one word

b

Move cursor left one word

e

Move cursor to next word ending

W, B, E

Like w, b, and e, but different word definition

^ or CTRL-A

Move cursor to beginning of line (first non-whitespace character)

0

Move cursor to beginning of line

$ or CTRL-E

Move cursor to end of line

w, b, and e stop at whitespace or punctuation. W, B, and E stop at whitespace only.

Table B-6. Character-seeking motion commands (command mode)

Command

Description

fc

Move cursor to next instance of c in line

Fc

Move cursor to previous instance of c in line

tc

Move cursor up to next instance of c in line

Tc

Move cursor back to previous instance of c in line

;

Repeat previous f or F command

,

Repeat previous f or F command in opposite direction

Table B-7. Text insertion commands (command mode)

Command

Description

a

Append new text after cursor until ESC

i

Insert new text before cursor until ESC

A

Append new text after end of line until ESC

I

Insert new text before beginning of line until ESC

Table B-8. Text deletion commands (command mode)

Command

Description

X

Delete character under cursor

X or DEL

Delete character to left of cursor

dm

Delete from cursor to end of motion command m

D

Synonym for d$

CTRL-W

Delete word backward

CTRL-U

Delete from beginning of line to cursor

CTRL-K

Delete from cursor to end of line

Table B-9. Text replacement commands (command mode)

Command

Description

cm

Change characters from cursor to end of motion command m until ESC

C

Synonym for c$

rc

Replace character under cursor with character c

R

Replace multiple characters until ESC

s

Substitute character under cursor with characters typed until ESC

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.

The bindkey Command

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
bindkey key           List binding for key
bindkey key cmd       Bind key to editing command cmd
bindkey -c key cmd    Bind key to UNIX command cmd
bindkey -s key str    Bind key to string str
bindkey -r key        Remove binding for key

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

-a

Key binding applies to alternate key map (vi command mode key map)

-k

Allows key to be up, down, left, or right to indicate an arrow key

-b

Allows key to be C-X or M-X to indicate CTRL-X or META-X

--

If key begins with -, a preceding -- flag tells bindkey not to process key as a flag

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

^X

CTRL-X

^[

ESC

^?

DEL

^

^

a

CTRL-G (bell)



CTRL-H

e

ESC

f

FORMFEED

NEWLINE

RETURN

TAB

v

CTRL-K (vertical tab)

nnn

ASCII character with octal code nnn

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset