Go to the previous, next section.
History substitution allows you to use words from previous command lines
in the command line you are typing. This simplifies spelling
corrections and the repetition of complicated commands or arguments.
Command lines are saved in the history list, the size of which is
controlled by the HISTSIZE
variable. The most recent command is
retained in any case. A history substitution begins with a !
and
may occur anywhere on the command line; history substitutions do not
nest. The !
can be escaped with \
to suppress its special
meaning. Single or double quotes will not work for this.
Input lines containing history substitutions are echoed on the terminal after being expanded, but before any other substitutions take place or the command gets executed.
An event designator is a reference to a command-line entry in the history list.
!
=
, or (
.
!!
!n
!-n
!str
!?str[?]
!#
!{...}
A word designator indicates which word or words of a given command line
will be included in a history reference. A :
separates the event
specification from the word designator. It can be omitted if the word
designator begins with a ,
, $
, *
, -
or
%
. Word designators include:
0
n
^
1
.
$
%
?str
search.
x-y
-y
abbreviates 0-y
.
*
x*
x-$
.
x
x*
but omitting word $
.
Note that a %
word designator will only work when used as
!%
, !:%
, or !?str?:%
, and only when used
after a ?!
substitution. Anything else will result in an error,
although the error may not be the most obvious one.
After the optional word designator, you can add a sequence of one or
more of the following modifiers, each preceded by a :
. These
modifiers also work on the result of filename and parameter
expansion.
h
r
.xxx
, leaving the
basename.
e
t
&
g
g&
).
p
q
x
q
, but break into words at each blank.
l
u
f
F:expr:
f
, but repeats only n times if the expression
expr evaluates to n. Any character can be used instead of
the :
, with the exception that if (
, [
, or
{
is used as the opening delimiter, the closing delimiter must
be )
, ]
, or }
respectively.
w
W:sep:
w
, but words are considered to be the parts of the string
that are separated by sep. Delimiters are handled as in F
above.
s/l/r[/]
Unless preceded by a g
, the substitution is done only for the
first string that matches l.
The left-hand side of substitutions are not regular expressions, but
character strings. Any character can be used as the delimiter in place
of /
. A backslash quotes the delimiter character. The character
&
, in the right-hand side, is replaced by the text from the
left-hand side. The &
can be quoted with a backslash. A null
l uses the previous string either from a l or from a
contextual scan string s from !?s
. You can omit the
rightmost delimiter if a newline immediately follows r; the
rightmost ?
in a context scan can similarly be omitted.
By default, a history reference with no event specification refers to
the same line as the last history reference on that command line, unless
it is the first history reference in a command. In that case, a history
reference with no event specification always refers to the previous
command. However, if the option CSH_JUNKIE_HISTORY
is set, then
a history reference with no event specification will always refer
to the previous command. For example, !!:1
will always refer to
the first word of the previous command and !!$
will always refer
to the last word of the previous command. If CSH_JUNKIE_HISTORY
is set, then !:1
and !$
will function in the same manner
as !!:1
and !!$
, respectively.
However, if CSH_JUNKIE_HISTORY
is unset, then !:1
and
!$
will refer to the first and last words respectively of the
last command referenced on the current command line. However, if they
are the first history reference on the command line, then they refer to
the previous command.
The character sequence `^foo^bar' repeats the last command, replacing the string `foo' with the string `bar'.þ þ
If the shell encounters the character sequence !"
in the input,
the history mechanism is temporarily disabled until the current list is
fully parsed. The !"
is removed from the input, and any
subsequent !
characters have no special significance.þ
A less convenient but more comprehensible form of command history
support is provided by the fc
builtin
(see section Shell Builtin Commands).
Go to the previous, next section.