Mikhail R. Mikhail R. - 4 months ago 6
Linux Question

A command to run the command that was executed right before the most recent one?

In bash, to run the most recent command you used, you can either press the up arrow key on your keyboard and press

Enter
or type
!!
and press
Enter
. But, is there a command that you can use to run the command that was executed right before the most recent one? For example:

$ pwd
/home/john
$ ls
bin Documents Music Pictures Templates
Desktop Downloads output Public Videos


Typing in
!!
and pressing
Enter
will run the
ls
command again. But, is there something similar to the
!!
command that will let you run the
pwd
command in the same manner? If there is one, give some more details as to how it works.

Answer

You can use !n, where n is line number in your history. To get the second to last command you can then use !-2.