Skip to content
Home » Last Command: Your Instant Command History

Last Command: Your Instant Command History

The history command is a lifesaver, as it shows you a list of all the commands you’ve typed in the current shell session. However, what if you just want to see the last command you executed without having to scroll through the entire list? Enter the !! (or “bang bang”) command, also known as the “last command.”

Practical Use-Cases

Here are just a few examples of how the !! command can make your life easier:

Repeating the Last Command

Imagine you ran a command, which you need few times, for example:

$ ping -c 6

Instead of retyping the entire command, you can simply use !! to repeat the last command. Same as using arrow-up:

$ !!
ping -c 6

Combining with Other Bash Shortcuts

The !! command can also be combined with other bash shortcuts for even greater efficiency. For example, let’s say you want to run the last command as root:

$ sudo !!

Or, if you want to run the last command in the background:

$ !! &

5 Tips for Using the Bash Last Command

  1. Use the history command to see the entire list of commands you’ve executed in the current shell session, and find the number of the command you want to repeat. Then use !{number} to repeat that specific command.
  2. Use the !$ (or “bang dollar”) shortcut to repeat the last argument of the previous command. For example, cd !$ will take you to the last directory you were in.
  3. Use the !-{number} shortcut to repeat a command a certain number of commands ago. For example, !-2 repeats the command two commands ago.
  4. Use the ctrl + r key combination to search through your command history interactively.
  5. Combine the !! command with other bash shortcuts and commands for even greater efficiency, such as sudo, &&, and ;.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 2 =