< Day Day Up > |
2.6. History ExpansionIf 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.
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. 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.
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 > |