Use Last Instance of Terminal Command

The bash history is one of the most useful things you will find on your command line. For example, most people know that typing !! will perform the last command. This is especially useful if you try to run a command and it requires “sudo” privileges. Simply type sudo !! and you’re good to go.

There is another less well known command with a single “!”. We can actually execute the last instance of a command from the history by adding a “!” to the start of it. For example, I could run:

Now my bash would execute the last instance of an SSH command that I ran. I find this specific use of this trick particularly useful since I often SSH into the same server for work, and I don’t want to type out the whole command with the IP address every time I want to run it.

Hopefully you enjoyed this little snippet! I would love to hear about what kind of command line history you use in the comments below. As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future tutorials in the comments section below.