Bash History Tips and Tricks

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.

Event Designators:

  1. ! Start a history substitution, except when followed by a blank, newline, carriage return,  = or (
  2. !n It refers to n’th command in the history. So if you just type in
    !68 at the command prompt, 68th command in your history list will be executed.
  3. !-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.
  4. !! 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
  5. !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.
  6. !?string[?] It refers to the most recent command containing string. The trailing ? may be omitted if string is immediately followed by a newline.

Word Designators:

  1. 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
  2. ^ 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:
    $cat ~/.bashrc
    $vi !!^        //This is equivalent to "vi !!:1" that is vi ~/.bashrc
  3. $ This refers to the last word.
  4. x-y This refers to a range of words; ‘-y‘ is equivalent to ’0-y’.
  5. * 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.
  6. 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
$vi !:1
// This is equivalent to vi !!:1 or vi ~/.bashrc

Modifiers:

  1. h This removes the trailing file name component, leaving the head. Example:
    $cat /home/spsneo/.bashrc
    $ls !!:1:h
    //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.
  2. t This removes all leading file name components, leaving the tail.
  3. r This removes the trailing suffix of the form .xxx , leaving the basename.
  4. p Print the new command but do not execute it.
  5. s/old/new This substitutes the first occurrence of old with new. Example:
    $cat /etc/resolv.conf
    $!!:s/resolv/yum
    //This expands to cat /etc/yum.conf
  6. 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
    $!!:gs/test/source/
    //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.

One thought on “Bash History Tips and Tricks

Leave a Reply