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
Let’s say you tried to
/etc/shadow file as a normal user.
$ 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.
$ sudo !!
!!‘ 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’.
$ sudo !-1
!-1‘ syntax means refer to the previous command
n is 1 in this case.