How can I `alias sudo !!`?
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
Related videos on Youtube
Manishearth
Updated on September 18, 2022Comments
-
Manishearth almost 2 years
I'm trying to set an alias for
sudo !!
in Bash. I triedalias sbb='sudo !!
', but it interprets that as a literal!!
and printssudo: !!: 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? `
-
Admin about 10 years
-
Admin over 9 yearsPossible same on SU: superuser.com/questions/240180/…
-
-
text almost 11 yearsGot one for tcsh?
-
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 (substitutedsbb
for!!
); and in the end it turned out that what I wanted wasalias 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 almost 11 yearsThanks! This alias is great when as administrator you forget to put sudo in front of your command.
-
Anto over 9 years
please
is definitely the way to go ! -
Michael Durrant over 8 yearsworks for bash too so removed the 'for zsh' comment.
-
muru over 8 yearsPrevious answers already cover
history -p
andfc
. Anything new here? -
muru over 8 yearsOk, What of it?
-
Scott - Слава Україні over 8 yearsI find the
-ln -n -n
to be intensely confusing; also the bit about'
or''
(and also the bit about-<n>
). -
rubo77 about 8 yearsI tried
apt-get install thefuck
which exists, but it doesn't work on my Ubuntu 15.10 ;( -
rubo77 almost 8 yearsinstead of
sbb
I would preferprease
as alias as suggested by @jpschorr -
Frax over 5 yearsWorks well in zsh. fc is the command to use. Thanks
-
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 - likefuck
;) -
lucidbrot about 3 years
pls
, becauseplease
but also I'm salty -
Deoxal almost 3 yearsIs this functionally equivalent to Giles 2nd version? I'm not sure if they'll both work or if there might be slight differences.
-
alper over 2 years
usage: sudo -h | -K | -k | -V