Previous Section  < Day Day Up >  Next Section

2.6. History Expansion

If you are a C shell user, you may be familiar with the history expansion mechanism that it provides. bash provides a similar set of features. History expansion is a primitive way to recall and edit commands in the history list. The way to recall commands is by the use of event designators. Table 2-15 gives a complete list.

Table 2-15. Event designators

Command

Description

!

Start a history substitution

!!

Refers to the last command

!n

Refers to command line n

!-n

Refers to the current command line minus n

!string

Refers to the most recent command starting with string

!?string?

Refers to the most recent command containing string; the ending ? is optional

^string1^string2

Repeat the last command, replacing string1 with string2


By far the most useful command is !!. Typing !! on the command line re-executes the last command. If you know the command number of a specific command, you can use the !n form, where n is the command number. Command numbers can be determined from the history command. Alternatively, you can re-execute the most recent command beginning with the specified string by using !string.

You might also find the last expansion in the table to be of some use if you've made a typing mistake. For example, you might have typed:

$ cat through_the_loking_glass | grep Tweedledee > dee.list

Instead of moving back to the line and changing loking to looking, you could just type ^lok^look. This will change the string lok to look and then execute the resulting command.

It's also possible to refer to certain words in a previous command by the use of a word designator. Table 2-16 lists available designators. Note that when counting words, bash (like most UNIX programs) starts counting with zero, not with one.

Table 2-16. Word designators

Designator

Description

0

The zeroth (first) word in a line

n

The nth word in a line

^

The first argument (the second word)

$

The last argument in a line

%

The word matched by the most recent ?string search

x-y

A range of words from x to y. -y is synonymous with 0-y

*

All words but the zeroth (first); synonymous with 1-$.; if there is only one word on the line, an empty string is returned

x*

Synonymous with x-$

x-

The words from x to the second to last word


The word designator follows the event designator, separated by a colon. You could, for example, repeat the previous command with different arguments by typing !!:0 followed by the new arguments.

Event designators may also be followed by modifiers. The modifiers follow the word designator, if there is one. Table 2-17 lists the available modifiers.

Table 2-17. Modifiers

Modifier

Description

h

Removes a trailing pathname component, leaving the head

r

Removes a trailing suffix of the form .xxx

e

Removes all but the trailing suffix

t

Removes all leading pathname components, leaving the tail

p

Prints the resulting command but doesn't execute it

q

Quotes the substituted words, escaping further substitutions

x

Quotes the substituted words, breaking them into words at blanks and newlines

s/old/new/

Substitutes new for old


More than one modifier may be used with an event designator; each one is separated by a colon.

History expansion is fine for re-executing a command quickly, but it has been superseded by the command-line editing facilities that we looked at earlier in this chapter. Its inclusion is really only for completeness, and we feel you are better off mastering the techniques offered in the vi or emacs editing modes.

    Previous Section  < Day Day Up >  Next Section