Shell – Exclamation Mark !

The exclamation mark will definitely speed up your history look up. Usually people look up history by hitting the up arrow to go through the history commands. I literally have seen someone who hit the arrow sign more than 20 times and still wouldn’t be able to locate the exact command he was looking for.

Then there are some users there who uses the ‘history’ command to lookup history. One can either by copy paste the command, or you can find the command line number and use “!<number>” to execute the command.

Also you have people use command like ‘history | grep <keyword>’. However, if you happen to know the command you are searching for start with certain prefix or even contains certain keyword. You can use “!<prefix>” or “!?<substring>?” to quickly pull the last executed command that starts or contains the specified keyword.

(note: !xxx show in the history, the command it represents does)

 

$ echo 'hello'
hello
$ cd ~
$ !ec
echo 'hello'
hello
$ !hell
-bash: !hell: event not found
$ !?hell?
echo 'hello'
hello

Here is an answer on stackexchange that contains a more detailed explanation of use cases for exclamation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s