Recalling and running earlier commands
Retrieving a whole command line
Retrieving individual words
History short-form
See also
The history mechanism lets you recall and rerun earlier commands. To see what it’s remembered, use the history command or its usual alias h, which might show you something like the following:
The history list won’t be quite what you typed: it’ll be broken into separate words wherever one of the special tokens, &, |, ;, <, >, (, ), &&, ||, >> or <<, occurs. Only inline data escapes being broken up into words this way. Each command may be thought of as an array of words, indexed from 0.
To reuse the text or maybe just a few words from a previous command, you type an exclamation point ! followed by a few characters to identify what you want to reuse. You can do this anywhere and whatever you select is just stuffed back on the command line to be interpreted as whatever the context suggests. For convenience, the exclamation point is not treated as a history reference if it’s followed by white space (a space, tab or newline) or by =, ~ or (.
There several ways of picking up a whole command line. You already know how to do it interactively with arrow keys and command completion. You can also use a shorthand notation that can be more convenient if you want to do something a bit more complex. The simplest shorthand is !!, which picks up the text of the immediately preceding command:
The shell first echoes your command showing the effects of the substitutions and then runs it. The other quick ways of referring to a whole command line from history are by the command number,
relative to the immediately preceding command,
or by mentioning some of the text to look for. A question mark after the exclamation point means you’ll accept the match anywhere on the line; otherwise it has to be at the start.
A search string ends at the first word boundary. This is so it’s convenient to type additional text following without having it be confused as part of the search string. For example:
To pick off individual words of the immediately preceding command, there’s some convenient shorthand. !* gets all the argument words:
Notice that a history substitution can be smashed right up against other literal text.
In the chapter on editing, additional facilities for selecting individual words or doing a search/replace will be introduced.
Recognizing how frequently one would like to make a simple change to the immediately preceding command to correct a typo, the history mechanism provides a short form for just that purpose. % typed as the first character on the command line indicates that a search/replace pair follows:
Typing %% matches the beginning of the line:
It’s also possible to refer to the search string in the replacement string by using an ampersand. (This example also illustrates that the trailing % isn’t required unless you want to explicitly mark the end of the replacement.)
Obviously, that raises the question: how do you put a literal ampersand in the replacement? Simple. Just quote it with ^, the shell escape character.
History recall
Order of evaluation