History Expansion
If you are a C shell user, you may be familiar with the history expansion mechanism that it provides. bash provides a similar set of features. History expansion is a primitive way to recall and edit commands in the history list. The way to recall commands is by the use of event designators. Table 2.15 gives a complete list.
Table 2-15. Event Designators
| Command | Description |
|---|---|
| ! | Start a history substitution |
| !! | Refers to the last command |
| ! n | Refers to command line n |
| !- n | Refers to the current command line minus n |
| ! string | Refers to the most recent command starting with string |
| !? string? |
Refers to the most recent command containing string. The ending ? is optional |
| ^ string1^string2 |
Repeat the last command, replacing string1 with string2 |
By far the most useful command is !!. Typing !! on the command line re-executes the last command. If you know the command number of a specific command, you can use the !n form, where n is the command number. Command numbers can be determined from the history command. Alternatively, you can re-execute the most recent command beginning with the specified string by using ! string.
You might also find the last expansion in the table to be of some use if you’ve made a typing mistake. For example, you might have typed
$ cat through_the_loking_glass | grep Tweedledee > dee.listInstead of moving back to the line and changing loking to looking, you could just type ^lok^look. This will change the string lok to look and then execute the ...