Search code examples
bash

Repeat last command with "sudo"


I often forget to run commands with sudo. I'm looking for a way to make a bash function (or alias) for repeating the last command with sudo. Something like:

S() {
    sudo $(history 1)
}

Any ideas?


Solution

  • You can write:

    sudo !!
    

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