Objective: You wanted to run a command as root but you forgot to use sudo
. There is a useful shell shortcut (using Event Designators) to re-run the previous command under sudo
.
Let’s say you tried to cat
the /etc/shadow
file as a normal user.
1 2 |
$ cat /etc/shadow cat: /etc/shadow: Permission denied |
You will get permission denied, because the shadow file is only readable by root. To repeat the last command with sudo
, simply use the following syntax.
1 |
$ sudo !! |
The ‘!!
‘ syntax is a reference to a command line entry in the history list. It refers to the previous command. An alternative way to run the previous command is to use ‘!-1’.
1 |
$ sudo !-1 |
The ‘!-1
‘ syntax means refer to the previous command -n
, where n
is 1 in this case.