9.3 History Expansion

The shell provides a history expansion feature that is similar to the history expansion provided by csh (also referred to as history substitution where appropriate). This section describes the syntax used to manipulate the history information.

History expansion is enabled by default for interactive shells, and can be disabled using the +H option to the set builtin command (see The Set Builtin). Non-interactive shells do not perform history expansion by default, but it can be enabled with set -H.

History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

History expansion is performed immediately after a complete line is read, before the shell breaks it into words, and is performed on each line individually. Bash attempts to inform the history expansion functions about quoting still in effect from previous lines.

History expansion takes place in two parts. The first is to determine which entry from the history list should be used during substitution. The second is to select portions of that entry to include into the current one.

The entry selected from the history is called the event, and the portions of that entry that are acted upon are words. Various modifiers are available to manipulate the selected words. The entry is split into words in the same fashion that Bash does when reading input, so that several words surrounded by quotes are considered one word. The event designator selects the event, the optional word designator selects words from the event, and various optional modifiers are available to manipulate the selected words.

History expansions are introduced by the appearance of the history expansion character, which is ‘!’ by default. History expansions may appear anywhere in the input, but do not nest.

History expansion implements shell-like quoting conventions: a backslash can be used to remove the special handling for the next character; single quotes enclose verbatim sequences of characters, and can be used to inhibit history expansion; and characters enclosed within double quotes may be subject to history expansion, since backslash can escape the history expansion character, but single quotes may not, since they are not treated specially within double quotes.

When using the shell, only ‘\’ and ‘'’ may be used to escape the history expansion character, but the history expansion character is also treated as quoted if it immediately precedes the closing double quote in a double-quoted string.

Several characters inhibit history expansion if found immediately following the history expansion character, even if it is unquoted: space, tab, newline, carriage return, ‘=’, and the other shell metacharacters.

There is a special abbreviation for substitution, active when the quick substitution character (described above under histchars) is the first character on the line. It selects the previous history list entry, using an event designator equivalent to !!, and substitutes one string for another in that entry. It is described below (see Event Designators). This is the only history expansion that does not begin with the history expansion character.

Several shell options settable with the shopt builtin (see The Shopt Builtin) modify history expansion behavior If the histverify shell option is enabled, and Readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the Readline editing buffer for further modification. If Readline is being used, and the histreedit shell option is enabled, a failed history expansion is reloaded into the Readline editing buffer for correction.

The -p option to the history builtin command shows what a history expansion will do before using it. The -s option to the history builtin may be used to add commands to the end of the history list without actually executing them, so that they are available for subsequent recall. This is most useful in conjunction with Readline.

The shell allows control of the various characters used by the history expansion mechanism with the histchars variable, as explained above (see Bash Variables). The shell uses the history comment character to mark history timestamps when writing the history file.