Go to the previous, next section.

þ

History

þ

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.

þ þ

Event Designators

An event designator is a reference to a command-line entry in the history list.

!
Start a history substitution, except when followed by a blank, newline, =, or (.

!!
Refer to the previous command. By itself, this substitution repeats the previous command.

!n
Refer to command line n.

!-n
Refer to the current command line minus n.

!str
Refer to the most recent command starting with str.

!?str[?]
Refer to the most recent command containing str.

!#
Refer to the current command line typed in so far.

!{...}
Insulate a history reference from adjacent characters (if necessary).

þ þ

Word Designators

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
The first input word (command).

n
The n'th argument.

^
The first argument, that is, 1.

$
The last argument.

%
The word matched by (the most recent) ?str search.

x-y
A range of words; -y abbreviates 0-y.

*
All the arguments, or a null value if there is just one word in the event.

x*
Abbreviates x-$.

x
Like 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.

þ þ

Modifiers

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
Remove a trailing pathname component, leaving the head.

r
Remove a trailing suffix of the form .xxx, leaving the basename.

e
Remove all but the suffix.

t
Remove all leading pathname components, leaving the tail.

&
Repeat the previous substitution.

g
Apply the change to the first occurrence of a match in each word, by prefixing the above (for example, g&).

p
Print the new command but do not execute it.

q
Quote the substituted words, escaping further substitutions.

x
Like q, but break into words at each blank.

l
Convert the words to all lowercase.

u
Convert the words to all uppercase.

f
Repeats the immediately-following (without a colon) modifier until the resulting word doesn't change any more. This modifier and the following four only work with parameter and filename expansion.

F:expr:
Like 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
Makes the immediately-following modifier work on each word in the string.

W:sep:
Like 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[/]
Substitute r for l.

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.