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 ! (exclamation point) 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 ! (exclamation point) with a \ (backslash) to cancel the exclamation point's special meaning. In addition, if you place the ! before a blank, tab, new-line character, = (equal sign), or ( (left parenthesis), history substitution does not occur.
History substitutions also occur when you begin an input line with a ^ (caret or circumflex). The shell echoes any input line containing history substitutions at the workstation before it executes that line.
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 starting from 1. The built-in history command produces output of the type:
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. 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 ! (exclamation point) 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, !! (double exclamation point) refers to the previous command; the command !! alone on an input line reruns the previous command.
To select words from an event, follow the event specification with a : (colon) and one of the following word designators (the words of an input line are numbered sequentially starting from 0):
You can omit the colon that separates the event specification from the word designator if the word designator begins with a ^ (circumflex), $ (dollar sign), * (asterisk), - (dash), or % (percent sign). You can also place a sequence of the following modifiers after the optional word designator, each preceded by a colon:
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 / (slash) delimits the original word (OldWord) and its replacement (NewWord). However, you can use any character as the delimiter. In the following example, using the % ( percent sign) as a delimiter allows a / (slash) to be included in the words:
s%/home/myfile%/home/yourfile%
The shell replaces an & (ampersand) with the OldWord text in the NewWord variable. In the following example, /home/myfile becomes /temp/home/myfile.
s%/home/myfile%/temp&%
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 new-line character follows immediately. Use the following modifiers to delimit the history list:
Unless the g modifier precedes the above modifiers, the change applies only to the first modifiable word.
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 ^ (circumflex). 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 previous command.
If necessary, you can enclose a history substitution in { } (braces) to insulate it from the characters that follow. For example, if you want to use a reference to the command:
ls -ld ~paul
ls -ld ~paula
use the following construction:
!{l}a
In this example, !{l}a looks for a command starting with l and appends a to the end.
Enclose strings in single and double quotation marks in order to prevent all or some of the substitutions that remain. Enclosing strings in ' ' (single quotation marks) prevents further interpretation, while enclosing strings in " " (double quotation marks) allows further expansion. In both cases, the text that results becomes (all or part of) a single word.