...

/

Reusing Previous Commands and Arguments

Reusing Previous Commands and Arguments

Learn how to reuse previous commands and arguments.

The !! command

By now, we’re likely familiar with the history command, and we’ve probably used the “Up” arrow key to recall a previous command we’ve typed. Way back in the chapter Introduction, we saw the !! command, which lets us rerun the previous command. This works because of a feature called history expansion, which we can use to rerun previous commands, extract arguments, or fix typos.

Let’s review the !! command before we get into the more advanced features. Let’s use the ls -lh command to get a list of the home directory:

ls -lh

We then use the !! command to execute the command again:

!!
Terminal 1
Terminal
Loading...

The ! command

The ! symbol is an event designator. It refers to some event in our history. !! is a shortcut for the last command, but ! followed by a number refers to the specific command ...