sudo !!
is one of my favourite bash command line shortcuts. The !!
command executes the previous command. By appending sudo
to the front, you can run the command you executed previously as superuser. Here's an example wherein I reload ufw:
james@projects:~$ ufw reload
ERROR: You need to be root to run this script
james@projects:~$ sudo !!
sudo ufw reload
Firewall reloaded
james@projects:~$
I used this command earlier this week in a deep coding session where I needed to run multiple commands as superuser throughout. !!
is more convenient than copying the previous command or using the Option-click shortcut (on macOS) to click to the start of a line in your terminal. While researching for this piece, I found that someone adapted the famous "sudo make me a sandwich" XKCD comic to use the !!
syntax. ^1
Tagged in bash.