As you already know, to view all the history entries, use the history command. This will display all the commands that were executed earlier along with the number for that command in the history table.

Bash History Event Designators

1. Execute a specific command from the history using !n

If you’ve executed a command earlier, instead of re-typing it again, you can quickly execute it by using the corresponding number of the command in the history.

For example, to execute command #4, do the following. This will display command #4 from the history, and execute it immediately.

$ !4
service sshd restart

To execute a command that was typed 2 commands back, do the following.

$ !-2

To execute the previous command, do any one of the following:

$ !!
$ !-1

You can also press <Ctrl>-P (if you are in the default emacs mode) to get to the previous command.

If you’ve enabled vi style editing for the command line using ‘set -o vi’, use <Esc>-k to get to the previous command.

2. Execute a command with keywords using !string and !?string

You can also use keywords to execute a command from the history.

The following example will search for previous command that STARTS with the keyword “ps” and execute it. In this example, it picks up the previous command “ps -ef | grep http” and executes it.

$ !ps
ps -ef | grep http

The following example will search for previous command that CONTAINS the keyword “apache” and execute it. In this example, it picks up the previous command “/usr/local/apache2/bin/apachectl restart” and executes it.

$ !?apache
/usr/local/apache2/bin/apachectl restart

3. Replace a string from the previous command using ^str1^str2^

In the following example, first we executed the ls command to verify a file. Later we realized that we want to view the content of the file. Instead of typing the whole file name again, we can just replace the “ls” in the previous command with “cat” as shown below.

Note: For additional bash history tips, refer to 15 Examples To Master Linux Command Line History. This explains how to display timestamp in the history, and how to use various history related environment variables including HISTTIMEFORMAT, HISTSIZE, HISTFILE, HISTCONTROL, and HISTIGNORE

Bash History Word Designators

Word designators are very helpful when you want to type a new command, but use the argument from one of the command that was executed earlier. Some of the examples are shown below.

4. Get the 1st argument of a command using :^

In the following example, “!cp:^” was given as an argument to “ls -l” command. “!cp:^” locates the previous command in the history that starts with “cp” and gets the 1st argument of that command.

$ cp /etc/passwd /backup
$ ls -l !cp:^
ls -l /etc/passwd

The following example gets the 1st argument from the previous command.

$ ls -l !!:^

5. Get the last argument of a command using :$

In the following example, “!cp:$” was given as an argument to “ls -l” command. “!cp:$” locates the previous command in the history that starts with “cp” and gets the last argument of that command.

$ cp /etc/passwd /backup
$ ls -l !cp:$
ls -l /backup

The following example gets the last argument from the previous command.

$ls -l !!:$

6. Get the nth argument of a command using :n

In the following example, “!tar:2” was given as an argument to “ls -l” command. “!tar:2” locates the previous command in the history that starts with “tar” and gets the 2nd argument of that command.

13. Sed like Substitution in bash history using :s/str1/str2/

Instead of using the “^original^replacement^” as we discussed earlier, we can also use a sed like substitution in the bash history as shown in the example below. This might be easy to remember. !! is to call previous command, “:s/original-string/replacement-string/” is the sed-like syntax to replace a string.

$ !!:s/ls -l/cat/

You can also use the g flag (along with s flag) to do global substitution as shown below. This is helpful when you’ve mistyped multiple word and would like to change all of them together and execute the command again.

In the following example, by mistake I’ve given “password” twice (instead of passwd).

$ cp /etc/password /backup/password.bak

To fix this, just do the following global history sed like substitution.

$ !!:gs/password/passwd/
cp /etc/passwd /backup/passwd.bak

14. Repeat the substitution quickly using :&

If you’ve already executed a bash history substitution successfuly as shown above, you can repeat the same substitution quickly again using :&.

I’ve by mistake typed “password” again instead of “passwd” in another command.

$ tar cvf password.tar /etc/password

Now, instead of retyping the command, or doing the “gs/password/passwd”, I can just use “:&”, which will reuse the last substitution. Use “:g&” for reusing the last subsitution by globally.

$ !!:g&
tar cvf passwd.tar /etc/passwd

15. Print the command without executing it using :p

This is very helpful when you are doing complex history substitution, and you want to view the final command before executing it.

In the following example, “!tar:3-:p”, doesn’t really execute the command.

Since we’ve given “:p” here, it just does the substitution and displays the new command. Once you’ve verified the bash history expansion, and if you think this is the command you intended to run, remove the “:p” and execute it again.

Personally, I’m afraid to use some of these designators. It looks too easy to make an error and execute something I didn’t intend. But I guess point number #15 (Print the command without executing it using) addresses that.

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Contact Us

Email Me :
Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!.