Linux tip: Using an exclamation point (!) to reference events

This content is excerpted from the new 3rd Ed. of 'A Practical Guide to Linux: Commands, Editors, and Shell Programming', authored by Mark Sobell, ISBN 013308504X, published by Pearson/Prentice Hall Professional, Sept. 2012, Copyright 2013 Mark G. Sobell. For more info please visit or the publisher site,

exclamation pointflickr/Jose.Madrid

The C Shell history mechanism uses an exclamation point to reference events. This technique, which is available under

, is frequently more cumbersome to use than
but nevertheless has some useful features. For example, the !! command reexecutes the previous event, and the shell replaces the !$ token with the last word from the previous command line.

You can reference an event by using its absolute event number, its relative event number, or the text it contains. All references to events, called event designators, begin with an exclamation point (!). One or more characters follow the exclamation point to specify an event.

You can put history events anywhere on a command line. To escape an exclamation point so the shell interprets it literally instead of as the start of a history event, precede it with a backslash (\) or enclose it within single quotation marks.

Event Designators

An event designator specifies a command in the history list. Table 8-8 lists event designators.

!! reexecutes the previous event

You can reexecute the previous event by giving a !! command. In the following example, event 45 reexecutes event 44:

44 $ ls -l text -rw-rw-r--. 1 max pubs 45 04-30 14:53 text 45 $ !! ls -l text -rw-rw-r--. 1 max pubs 45 04-30 14:53 text

The !! command works whether or not your prompt displays an event number. As this example shows, when you use the history mechanism to reexecute an event, the shell displays the command it is reexecuting.

!n event number

A number following an exclamation point refers to an event. If that event is in the history list, the shell executes it. Otherwise, the shell displays an error message. A negative number following an exclamation point references an event relative to the current event. For example, the command !–3 refers to the third preceding event. After you issue a command, the relative event number of a given event changes (event –3 becomes event –4). Both of the following commands reexecute event 44:

51 $ !44 ls -l text -rw-rw-r--. 1 max pubs 45 04-30 14:53 text 52 $ !-8 ls -l text -rw-rw-r--. 1 max pubs 45 04-30 14:53 text

!string event text

When a string of text follows an exclamation point, the shell searches for and executes the most recent event that began with that string. If you enclose the string within question marks, the shell executes the most recent event that contained that string. The final question mark is optional if a

would immediately follow it.

68 $ history 10     59 ls -l text*     60 tail text5     61 cat text1 text5 > letter     62 vim letter     63 cat letter     64 cat memo     65 lpr memo     66 pine zach     67 ls -l     68 history 69 $ !l ls -l ... 70 $ !lpr lpr memo 71 $ !?letter? cat letter ...


InformIT is running a holiday promotion offering 40% off most books & eBook & video training packages from its site (Expires December 28, 2012 at 11:59 p.m. Eastern Standard Time). Click here for info on the discount coupon code.

ITWorld DealPost: The best in tech deals and discounts.