How can I `alias sudo !!`?

41,993

Solution 1

!! is expanded by bash when you type it. It's not expanded by alias substitution.

You can use the history built-in to do the expansion:

alias sbb='sudo $(history -p !!)'

If the command is more than a simple command (e.g. it contains redirections or pipes), you need to invoke a shell under sudo:

alias sbb='sudo "$BASH" -c "$(history -p !!)"'

Solution 2

Try:

alias sbb='sudo $(fc -ln -1)'

I like actually prefer to name it 'please':

alias please='sudo $(fc -ln -1)'

Info: fc is a in-built command in the bash shell. that lists, edits and reexecutes commands previously entered to an interactive shell.

Zsh has the similar issue with !! and aliases, and fc -ln -1 also works there. Though caveats about getting redirections and other shell syntax inside the sudo'ed command line apply, so you may want to wrap the command within a shell invocation or eval, see @rici's answer.

Solution 3

Now there's a repository for this purpose in GitHub, it magically checks whether it needs superuser rights, also fixes the typos:

https://github.com/nvbn/thefuck

Share:
41,993

Related videos on Youtube

Manishearth
Author by

Manishearth

Updated on September 18, 2022

Comments

  • Manishearth
    Manishearth almost 2 years

    I'm trying to set an alias for sudo !! in Bash. I tried alias sbb='sudo !!', but it interprets that as a literal !! and prints

    sudo: !!: command not found
    

    If I use double quotes, it substitutes the double bang in the string itself, so that doesn't work.

    Is there any way to make this work? Or an alternative alias? `

  • text
    text almost 11 years
    Got one for tcsh?
  • rici
    rici almost 11 years
    @mdpc: I don't use tcsh, but I glanced at man tcsh and discovered that it does do history expansion during alias expansion "as though the original command were the previous input line." Interesting. However, alias sbb 'sudo \!-2 did not work as expected (it actually substituted the second previous entry); alias sbb 'sudo \!\!' did work as expected (substituted sbb for !!); and in the end it turned out that what I wanted was alias sbb 'sudo \!-1' which I would have expected to be the same as \!\!. (tcsh 6.17.06 (Astron) 2011-04-15) YMMV. Good luck.
  • text
    text almost 11 years
    Thanks! This alias is great when as administrator you forget to put sudo in front of your command.
  • Anto
    Anto over 9 years
    please is definitely the way to go !
  • Michael Durrant
    Michael Durrant over 8 years
    works for bash too so removed the 'for zsh' comment.
  • muru
    muru over 8 years
    Previous answers already cover history -p and fc. Anything new here?
  • muru
    muru over 8 years
    Ok, What of it?
  • Scott - Слава Україні
    Scott - Слава Україні over 8 years
    I find the -ln -n -n to be intensely confusing; also the bit about ' or '' (and also the bit about -<n>).
  • rubo77
    rubo77 about 8 years
    I tried apt-get install thefuck which exists, but it doesn't work on my Ubuntu 15.10 ;(
  • rubo77
    rubo77 almost 8 years
    instead of sbb I would prefer prease as alias as suggested by @jpschorr
  • Frax
    Frax over 5 years
    Works well in zsh. fc is the command to use. Thanks
  • Przemek D
    Przemek D almost 5 years
    please makes it feel like the terminal is an entity that I have to be nice to in order to get stuff done. I prefer something that gives a better sense of control - like fuck ;)
  • lucidbrot
    lucidbrot about 3 years
    pls, because please but also I'm salty
  • Deoxal
    Deoxal almost 3 years
    Is this functionally equivalent to Giles 2nd version? I'm not sure if they'll both work or if there might be slight differences.
  • alper
    alper over 2 years
    usage: sudo -h | -K | -k | -V