gNU Bourne-Again SHell (GNU Bourne-Again SHell)
HISTORY EXPANSION
The shell supports a history expansion feature that is similar to
the history expansion in csh
. This section describes what syntax
features are available. This feature is enabled by default for
interactive shells, and can be disabled using the +H
option to
the set
builtin command (see SHELL BUILTIN COMMANDS
below). Non-
interactive shells do not perform history expansion by default.
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 without taking quoting on previous
lines into account. It takes place in two parts. The first is
to determine which line from the history list to use during
substitution. The second is to select portions of that line for
inclusion into the current one. The line selected from the
history is the event, and the portions of that line that are
acted upon are words. Various modifiers are available to
manipulate the selected words. The line is broken into words in
the same fashion as when reading input, so that several
metacharacter-separated words surrounded by quotes are considered
one word. History expansions are introduced by the appearance of
the history expansion character, which is !
by default. Only
backslash (\
) and single quotes can quote 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 =
. If the
extglob
shell option is enabled, (
will also inhibit expansion.
Several shell options settable with the shopt
builtin may be used
to tailor the behavior of history expansion. If the histverify
shell option is enabled (see the description of the shopt
builtin
below), 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 substitution will be
reloaded into the readline
editing buffer for correction. The -p
option to the history
builtin command may be used to see 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.
The shell allows control of the various characters used by the
history expansion mechanism (see the description of histchars
above under Shell Variables
). The shell uses the history comment
character to mark history timestamps when writing the history
file.
Event Designators
An event designator is a reference to a command line entry in the
history list. Unless the reference is absolute, events are
relative to the current position in the history list.
!
Start a history substitution, except when followed by a
blank
, newline, carriage return, = or ( (when the extglob
shell option is enabled using the shopt
builtin).
!
n Refer to command line n.
!-
n Refer to the current command minus n.
!!
Refer to the previous command. This is a synonym for
`!-1'.
!
string
Refer to the most recent command preceding the current
position in the history list starting with string.
!?
string[?]
Refer to the most recent command preceding the current
position in the history list containing string. The
trailing ?
may be omitted if string is followed
immediately by a newline. If string is missing, the
string from the most recent search is used; it is an error
if there is no previous search string.
^
string1^
string2^
Quick substitution. Repeat the previous command,
replacing string1 with string2. Equivalent to
``!!:s^string1^string2^'' (see Modifiers
below).
!#
The entire command line typed so far.
Word Designators
Word designators are used to select desired words from the event.
A :
separates the event specification from the word designator.
It may be omitted if the word designator begins with a ^
, $
, *
,
-
, or %
. Words are numbered from the beginning of the line, with
the first word being denoted by 0 (zero). Words are inserted
into the current line separated by single spaces.
0 (zero)
The zeroth word. For the shell, this is the command word.
n The nth word.
^
The first argument. That is, word 1.
$
The last word. This is usually the last argument, but
will expand to the zeroth word if there is only one word
in the line.
%
The first word matched by the most recent `?string?'
search, if the search string begins with a character that
is part of a word.
x-
y A range of words; `-y' abbreviates `0-y'.
*
All of the words but the zeroth. This is a synonym for
`1-$'. It is not an error to use *
if there is just one
word in the event; the empty string is returned in that
case.
x*
Abbreviates x-$.
x-
Abbreviates x-$ like x*
, but omits the last word. If x
is
missing, it defaults to 0.
If a word designator is supplied without an event specification,
the previous command is used as the event.
Modifiers
After the optional word designator, there may appear a sequence
of one or more of the following modifiers, each preceded by a
`:'. These modify, or edit, the word or words selected from the
history event.
h
Remove a trailing filename component, leaving only the
head.
t
Remove all leading filename components, leaving the tail.
r
Remove a trailing suffix of the form .xxx, leaving the
basename.
e
Remove all but the trailing suffix.
p
Print the new command but do not execute it.
q
Quote the substituted words, escaping further
substitutions.
x
Quote the substituted words as with q
, but break into
words at blanks
and newlines. The q
and x
modifiers are
mutually exclusive; the last one supplied is used.
s/
old/
new/
Substitute new for the first occurrence of old in the
event line. Any character may be used as the delimiter in
place of /. The final delimiter is optional if it is the
last character of the event line. The delimiter may be
quoted in old and new with a single backslash. If &
appears in new, it is replaced by old. A single backslash
will quote the &. If old is null, it is set to the last
old substituted, or, if no previous history substitutions
took place, the last string in a !?
string[?]
search. If
new is null, each matching old is deleted.
&
Repeat the previous substitution.
g
Cause changes to be applied over the entire event line.
This is used in conjunction with `:s
' (e.g.,
`:gs/
old/
new/
') or `:&
'. If used with `:s
', any delimiter
can be used in place of /, and the final delimiter is
optional if it is the last character of the event line.
An a
may be used as a synonym for g
.
G
Apply the following `s
' or `&
' modifier once to each word
in the event line.