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:

!!

Get hands-on with 1200+ tech skills courses.