[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

History Substitution in the C Shell

History substitution lets you modify individual words from previous commands to create new commands. History substitution makes it easy to repeat commands, repeat the arguments of a previous command in the current command, or fix spelling mistakes in the previous command with little typing.

History substitutions begin with the ! character and can appear anywhere on the command line, provided they do not nest (in other words, a history substitution cannot contain another history substitution). You can precede the ! with a \ to cancel the exclamation point's special meaning. In addition, if you place the ! before a blank, tab, newline character, =, or (, history substitution does not occur.

History substitutions also occur when you begin an input line with a ^. The shell echoes any input line containing history substitutions at the workstation before it executes that line.

This section discusses the following:

History Lists

The history list saves commands that the shell reads from the command line that consist of one or more words. History substitution reintroduces sequences of words from these saved commands into the input stream.

The history shell variable controls the size of the history list. You must set the history shell variable either in the .cshrc file or on the command line with the built-in set command. The previous command is always retained regardless of the value of the history variable. Commands in the history list are numbered sequentially, beginning with 1. The built-in history command produces output similar to the following:

9 write michael
10 ed write.c
11 cat oldwrite.c
12 diff *write.c

The shell displays the command strings with their event numbers. The event number appears to the left of the command and represent when the command was entered in relation to the other commands in the history. It is not usually necessary to use event numbers to refer to events, but you can have the current event number displayed as part of your system prompt by placing an ! in the prompt string assigned to the PROMPT environment variable.

A full history reference contains an event specification, a word designator, and one or more modifiers in the following general format:

Event[.]Word:Modifier[:Modifier] . . . 

Note: Only one word can be modified. A string that contains blanks is not allowed.

In the previous sample of history command output, the current event number is 13. Using this example, the following refer to previous events:

!10 Event number 10.
!-2 Event number 11 (the current event minus 2).
!d Command word beginning with d (event number 12).
!?mic? Command word containing the string mic (event number 9).

These forms, without further modification, simply reintroduce the words of the specified events, each separated by a single blank. As a special case, !! refers to the previous command; the command !! alone on an input line reruns the previous command.

Event Specification

To select words from an event, follow the event specification with a : and one of the following word designators (the words of an input line are numbered sequentially starting from 0):

0 First word (the command name)
n nth argument
^ First argument
$ Last argument
% Word matched by an immediately preceding ?string? search
x-y Range of words from the xth word to the yth word
-y Range of words from the first word (0) to the yth word
* First through the last argument, or nothing if there is only one word (the command name) in the event
x* xth argument through the last argument
x- Same as x* but omitting the last argument

If the word designator begins with a ^, $, *, -, or %, you can omit the colon that separates the event specification from the word designator. You can also place a sequence of the following modifiers after the optional word designator, each preceded by a colon:

h Removes a trailing path name extension, leaving the head.
r Removes a trailing .xxx component, leaving the root name.
e Removes all but the .xxx trailing extension.
s/OldWord/NewWord/ Substitutes the value of the NewWord variable for the value of the OldWord variable.

The left side of a substitution is not a pattern in the sense of a string recognized by an editor; rather, it is a word, a single unit without blanks. Normally, a / delimits the original word (OldWord) and its replacement (NewWord). However, you can use any character as the delimiter. In the following example, using the % as a delimiter allows a / to be included in the words:


The shell replaces an & with the OldWord text in the NewWord variable. In the following example, /home/myfile becomes /temp/home/myfile.


The shell replaces a null word in a substitution with either the last substitution or with the last string used in the contextual scan !?String?. You can omit the trailing delimiter (/) if a newline character follows immediately. Use the following modifiers to delimit the history list:

t Removes all leading path name components, leaving the tail
& Repeats the previous substitution
g Applies the change globally; that is, all occurrences for each line
p Displays the new command, but does not run it
q Quotes the substituted words, thus preventing further substitutions
x Acts like the q modifier, but breaks into words at blanks, tabs, and new-line characters

When using the preceeding modifiers, the change applies only to the first modifiable word unless the g modifier precedes the selected modifier.

If you give a history reference without an event specification (for example, !$), the shell uses the previous command as the event. If a previous history reference occurs on the same line, the shell repeats the previous reference. Thus, the following sequence gives the first and last arguments of the command that matches ?foo?.

!?foo?^ !$

A special abbreviation of a history reference occurs when the first nonblank character of an input line is a ^. This is equivalent to !:s^ , thus providing a convenient shorthand for substitutions on the text of the previous line. The command ^ lb^ lib corrects the spelling of lib in the command.

If necessary, you can enclose a history substitution in { } to insulate it from the characters that follow. For example, if you want to use a reference to the command:

ls -ld ~paul

to perform the command:

ls -ld ~paula

use the following construction:


In this example, !{l}a looks for a command starting with l and appends a to the end.

Quoting with Single and Double Quotes

To prevent further interpretation of all or some of the substitutions, enclose strings in single and double quotation marks. Enclosing strings in '  ' prevents further interpretation, while enclosing strings in "  " allows further expansion. In both cases, the text that results becomes all or part of a single word.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]