July 18, 2005

CLI Magic: Bash history expansion

Author: Shashank Sharma

This week's CLI Magic -- by Shashank Sharma -- is all about history expansion.
The Bourne-Again SHell (bash) has a major user base and the fact that it has incorporated some useful features from the Korn shell (ksh) and C shell (csh) might be one of the reasons. History expansion, for example, was first implemented in csh.Recalling commands

Entering the history command without any switches displays the history list with line numbers. If that's too much information, enter history N where N is the number of previously executed commands you want displayed.

Bash also allows for incremental search on the history list. Use ctrl+r for a backward/reverse search or ctrl+s to perform a forward search. As you type the first few letters of the command, the last command from the history list that matches with your string would be displayed.

Since you are at the command line, you can club combine some popular commands and cook up interesting surprises. One such example is to pass history through grep along with the first few letters of a command. For example, try this simple hack, which displays all the commands beginning with the letters you supply:

history | grep -i first-few-letters-of-command

History expansion

History expansion serves two purposes, the first is repeating a command from the history list. The second is selecting portions from a command for substituting into the current command.

History expansions are implemented by the history expansion character '!'. The line selected from the history list is called 'event' and portions of that command that are selected are called 'words'. Depending on your needs, you can either use 'word' or 'event designators'.

Event designators

Event designators, invoked by the history expansion character are used for command substitution, that is, repeating a command from the list. ! starts history substitution. It can be escaped with a backslash (\), since it is a special character.
In this example, history command 1008 -- a simple ls -- is repeated:

star trek

Another useful event designator is !string, which runs the most recent command beginning with the string you specify.

Word designators

Word designators are used to select specified words from an event. A colon (:) is used to separate word designators from events. Words in a command are numbered from the beginning of the line, with the first word denoted by zero (0). Once selected, a word is inserted into the current command separated by a single space.

Some common word designators are n, which refers to the nth word; ^ refers to the first word; $ refers to the last word and * refers to the complete line entry except for the first word, which is the command.

For example, !conv:5 displays the fifth word from the last command beginning with !conv, while !conv:$ would display the last word from the same command. Please note that !conv:* is same as !conv:1-$ - both resulting in the complete command except for the first word.

Modifying selected words

There are a number of modifiers that help you to describe how you want the selections to behave. Each modifier in a command needs to be preceeded with a colon (:) and you can use more than one modifier in a command. A very popular modifier is p which results in the command being listed instead of being executed. For example:

ls -l

There is also a search-and-replace type modifier that you can use when you wish to replace some text with some other. This modifier is s/old/new. For example:

ls -a

Note: A !! is used to refer to the last command in the history list. The last command was ls -l and so the -l was replaced with -a and the new command, ls -a was executed.


After encountering a thread in a popular forum board that just brushed on Bash and its event designators, I've since done enough reading from the man pages and the web to officially declare that it is a very cool feature which requires very little effort to get hooked on.

Shashank Sharma is a Computer Science student who loves to write about free and open source software for new users.

Click Here!