Bash supports history expansion feature which can be very useful for powerful command-line users.
According to bash manual page –
History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.
History expansion is composed of three parts –
- Event Designators: It is a reference to an entry in the history list.
- Word Designators: It is used to select a word from the event.
- Modifiers: Modifies the word selected by the word designator or the action of the command.
All the three parts are optional and are separated from each other by a : (colon). There can be multiple modifiers each separated by a : (colon).
Now I will explain each part one by one.
- ! Start a history substitution, except when followed by a blank, newline, carriage return, = or (
- !n It refers to n’th command in the history. So if you just type in
!68at the command prompt, 68th command in your history list will be executed.
- !-n It refers to n’th command in the history from last. So if you just type in !-1 at the command prompt, last command will be executed.
- !! is an alias for !-1. Example:
$apt-get install gnome-ppp //Permission Denied. You need to be sudo to do this!
$sudo !! // This is equivalent to sudo apt-get install gnome-ppp
- !string It refers to the most recent command in the history starting with string. It is again an useful expansion when you don’t remember the arguments to a command which you have executed earlier.
- !?string[?] It refers to the most recent command containing string. The trailing ? may be omitted if string is immediately followed by a newline.
- n The nth word, count starting from 0. 0th word normally refers to the command. Example:
$sudo cat /etc/resolv.conf //Instead you want to edit the resolv.conf file
$sudo vi !!:2 //This is equivalent to sudo vi /etc/resolv.conf
- ^ This refers to 1st word. This is equivalent to :1 as refered above. The only .advantage is that you can omit : (colon) when you use ^. Example:
$vi !!^ //This is equivalent to "vi !!:1" that is vi ~/.bashrc
- $ This refers to the last word.
- x-y This refers to a range of words; ‘-y‘ is equivalent to ‘0-y’.
- * This refers to all the words except the 0th one. This is helpful when you have to execute a command with all the arguments passed to the last command.
- x* This is an alias for x-$ .
Note: If a word designator is used without an event specification, the last command in the history is used as the event. Example –
$cat ~/.bashrc // This is equivalent to vi !!:1 or vi ~/.bashrc
- h This removes the trailing file name component, leaving the head. Example:
$cat /home/spsneo/.bashrc//This expands to
ls /home/spsneoExplanation: !! refers to the last command and then :1 refers to the 1st word of the last command and then :h removes the trailing file name component i.e., .bashrc . Hence the expansion.
- t This removes all leading file name components, leaving the tail.
- r This removes the trailing suffix of the form .xxx , leaving the basename.
- p Print the new command but do not execute it.
- s/old/new This substitutes the first occurrence of old with new. Example:
$cat /etc/resolv.conf//This expands to
- g This is used in conjunction with “:s” modifier. This causes changes to be applied over the entire event line rather than just the first occurrence. Example:
$cat test.cpp test.h//This expands to
cat source.cpp source.hh
This is all I have to share about bash history expansion.
Do post comments if you find any mistake or if you want any further clarifications. One can also go through the bash manual pages for more options.