brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: type sudo !! when you forgot the sudo

Linux
~/tips/tip-linux-sudo-bang-bang

sudo !!

Reruns your previous command with sudo in front, so you do not retype it.

We have all typed a long command and watched it die with permission denied. I used to press the up arrow and walk back to the start of the line. There is a shorter way.

In bash, !! expands to the previous command. Put sudo in front of it and the shell reruns the line as root.

$ apt update
E: Could not open lock file - Permission denied
$ sudo !!
sudo apt update

Bash prints the expanded line before it runs, so you see what you are about to do. If you are nervous, add :p (!!:p) and bash only prints the command and puts it in your history, without running it.

One caveat: it reruns exactly what you typed last, so be sure the last line is the one you mean. A destructive command with a sudo in front of it is still destructive. Zsh supports the same history expansion, so the habit travels.

This is the habit I would teach first to anyone new to the terminal. It costs nothing to learn and saves you a small irritation every day. Related shortcuts exist too: !$ gives you the last argument of the previous command, which is handy when you want to run something else on the same file.

Further reading: Bash manual: History Interaction, On this site: cd – takes you back to the last directory, On this site: sudo !! reruns your last command with sudo

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link