Execute last command as root

Sometimes you need to re-run a command you just ran, but this time as a super-user. For example, you may have run a command which needed super-user rights, but forgot to prepend the command with sudo.

In that case you can make use of the !! bash shortcut to re-run the previous command. But helpfully you can also prepend the !! shortcut with sudo. For example, the following will re-run the previously entered command as root user.

sudo !!

!! is automatically substituted to the content of the previous command. Turning the above line into sudo <your previous command here>.

Subscribe to my monthly newsletter

No spam, no sharing to third party. Only you and me.

Member discussion