Go to the first, previous, next, last section, table of contents.


History

When used interactively, the shell keeps a list of recently entered commands - known as the history. This history is accessable through History Expansion the use of which is described under Command Editting and though the history built in command.

The number of lines of history that is kept is governed by the HISTORY variable which defaults to 100.

The history is stored as lines of text, just as they were read in, with the location of the beginning of each word remembered. Thus, the exact entered text can be recovered, either as whole lines, or as sequences of one or more words.

History expansion is signalled by an exclamation point. As the processing of history expansion is done very early, the only quoting that can be used to disable history expansion is to precede the exclamation point with a slosh (sloshchar!).

Following this exclamation point is a history line identifier and a history word identifier. Either of these may be empty, and it may be necessary to separate them with a colon if they are not otherwise distinguishable.

The line identifier can be any one of the following:

Empty
This signifies the most recent command. If there is an earlier history expansion in the line, this refers to the same history line as that expansion did.
A Decimal number
Each line is numbered, with 1 being the first command entered. This identifier will use the history line with the given number, if it still exists.
A Hypen followed by a number
This will subtract the number from the number of the current history line and use the history line with the resulting number. Thus
!-1
gives the last command.
The prefix of some command
A string of characters other than !:$*?`"'()^&#[]{}| and white space is assumed to be a prefix for a recent command and the history is searched in reverse order to the first command with that prefix. That command line is then used.
A question mark followed by a string
Where the string is taken from the same set of characters as above, the history is searched in reverse order for the first line that contains this string, and that line is used.

The word identifier is one of the following where n is a decimal integer or a circumflex (^) indicating 1, and m is a decimal integer, a dollar sign ($), or a dollar sign followed by a hyphen and a decimal integer. If the dollar sign is used it refers to the maximal meaningful number. A dollar sign followed by a hyphen and a number means that number less than the maximal meaningful number.

`m'
Include just the numbered word. Word numbering starts at zero.
`n--m'
Includes the range of words from n to m inclusive. If n is ommited it defaults to 1, if m is ommitted it defaults to $--1.
`n*'
Equvalent to n-$. If n is ommited, it defaults to 1.
`!'
Include all words from the given history line. This is also the default if no word identifier is given.

As a special case, if both line identifier and word identifier are empty, no substitution is done.


Go to the first, previous, next, last section, table of contents.