While working in command terminal, we may want to re-enter a command we entered before. We can repeatedly press the up key on our keyboard until we find the wanted command, but sometimes it can be better to use the history command instead. This command lists all the commands we entered since the first terminal launch:
$ history
...
2009 date
2010 history
2011 uname
Each line have a number and a matching command.
We can re-enter those commands by simply typing:
$ !#
Where # should be substituted with the matching number of the command wanted to be re-executed in the command history.
$ !2009
date
Fri Oct 21 14:31:54 +03 2022
If the command you want to run requires sudo privileges, you can simply type:
$ `sudo !#`
PS: You can re-execute last command with super user privilegesvia the following command:
$ sudo !!