2.6. History Expansion

 < Day Day Up > 

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




Start a history substitution


Refers to the last command


Refers to command line n


Refers to the current command line minus n


Refers to the most recent command starting with string


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


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




The zeroth (first) word in a line


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


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


Synonymous with 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




Removes a trailing pathname component, leaving the head


Removes a trailing suffix of the form .xxx


Removes all but the trailing suffix


Removes all leading pathname components, leaving the tail


Prints the resulting command but doesn't execute it


Quotes the substituted words, escaping further substitutions


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


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.

     < Day Day Up > 

    Learning the bash Shell
    Learning the bash Shell: Unix Shell Programming (In a Nutshell (OReilly))
    ISBN: 0596009658
    EAN: 2147483647
    Year: 2005
    Pages: 139

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net