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? `


!! 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 !!)"'
  • Got one for tcsh? – mdpc Aug 2 '13 at 19:02
  • 1
    @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. – rici Aug 2 '13 at 21:10
  • Thanks! This alias is great when as administrator you forget to put sudo in front of your command. – mdpc Aug 2 '13 at 21:42
  • instead of sbb I would prefer prease as alias as suggested by @jpschorr – rubo77 Jul 7 '16 at 5:52


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.

  • 18
    please is definitely the way to go ! – Anto Mar 18 '15 at 16:36
  • works for bash too so removed the 'for zsh' comment. – Michael Durrant Oct 10 '15 at 14:35
  • 1
    Works well in zsh. fc is the command to use. Thanks – Frax Jan 7 '19 at 8:32
  • 1
    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 ;) – Przemek D Sep 11 '19 at 7:23

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


  • I tried apt-get install thefuck which exists, but it doesn't work on my Ubuntu 15.10 ;( – rubo77 Jun 15 '16 at 7:53

This was originally posted at this question but later, suggested to post here.

Here is how you can use fc and history with alias for !- like substitutions. Suppose if you want to use !-n (where n is digit for nth last command used), then you can use fc and history with alias as follows:

  1. Using history:

    alias xyz='echo $(history -p "!-<n>") ; sudo $(history -p "!-n")'

    Note: Here !-n should be enclosed with ' or ''.

  2. Using fc:

    alias xyz='echo $(fc -ln -n -n) ; sudo $(fc -ln -n -n)'

    Note: here 1st and 2nd -n are used to specify the range of history list.

Here I used echo-ing before executing command for felling like !-<n>.

  • Previous answers already cover history -p and fc. Anything new here? – muru Jan 5 '16 at 10:15
  • Ok, What of it? – muru Jan 5 '16 at 10:54
  • I find the -ln -n -n to be intensely confusing; also the bit about ' or '' (and also the bit about -<n>). – Scott Jan 7 '16 at 19:47
  • Option 2. must be a typo, I think you want $(fc -ln -1) – cmcginty Feb 12 '16 at 2:14
alias please="sudo $(history -p \!\-2)"

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.