Did you know?
sudo !! reruns your last command with sudo.
Handy when a command fails with "Permission denied".
- Shell & terminal
- Last checked
Explained
Forgot sudo again? Instead of retyping the whole command, type sudo !!. In Bash and Zsh, !! is history expansion for “the previous command”, so the shell turns it into the full command line with sudo in front.
apt install nginxsudo !!
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
sudo apt install nginx
[sudo] password for rahul:
Bash prints the expanded command before it runs it, so you can see exactly what is executed.
Other history shortcuts work the same way: !! can be combined with anything, for example echo "!!" >> notes.txt, and !ssh reruns the most recent command that started with ssh. Be careful with them in a hurry: if the last command was destructive, sudo !! repeats it with full privileges.
Learn more
-
sudocommand cheat sheet Options and copy-ready examples
