I run a command, then realize it should have been run with sudo.

In zsh I have a function so that I can hit a ^x^x and it will grab the previous line from the history and prepend sudo to it, so I can run it again.

Is it possible to do this with bash?


As mentioned by Gerald Schneider, this is a duplicate that exists on Stack Overflow, but since it doesn't exist on Serverfault, and it won't let me mark it as duplicate, I'll just put the answer here as well.

From jm666 question here

You can write:

sudo !!

(See §9.3 "History Expansion" in the Bash Reference Manual.)


Most bash implementations I have used allow command-history navigation via the up- and down-arrow keys.

So in addition to @trueCamelType's solution, one could also use:


Eight characters, either way. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.