Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I often forget to run a command with sudo, so I find myself often typing sudo !! immediately afterwards.

I tried aliasing this, but bash chokes on the !! part. Is there some way to represent this shortcut within an alias?

share|improve this question
    
That's not a wild-card that's a history expansion feature. History expansion is performed before alias expansion so when the alias is expanded the !! are considered literal. –  Dennis Williamson Feb 1 '11 at 1:54

2 Answers 2

up vote 3 down vote accepted

AIUI the problem is that history substitutions (!!) are done before alias substitution. I haven't tested this thoroughly, but it looks like fc can be used to get what you want:

alias sudothat='eval "sudo $(fc -ln -1)"'
share|improve this answer
1  
No need for the eval (plus it may have some undesirable effects). –  Dennis Williamson Feb 1 '11 at 1:38
    
Without the eval, it won't parse things like the quoted strings correctly, and with double-quotes around the results of fc it should evaluate everything exactly once -- which is what you want. –  Gordon Davisson Feb 1 '11 at 3:47
    
@Dennis Williamson: I've now tested this a bit more thoroughly, and it seems to work as expected (i.e. the same as typing sudo !!) for everything I've tried -- quoted arguments, pipelines, command substitutions, command substitutions containing command substitutions containing pipes... I don't see any way to do this right without an eval. –  Gordon Davisson Feb 19 '11 at 15:45

From a colleague at work:

alias sa='sudo `history -p \!\!`'

appears to do the trick

share|improve this answer
1  
it should be noted that this only works when using single quotes ('). –  Torian Feb 1 '11 at 3:42
    
This won't handle quoted arguments properly -- for example, if you use it to rerun echo "a   b" (which prints a   b), it'll print "a b" (as in, the quotes are actually included in the arguments passed to echo, but the spaces between aren't). –  Gordon Davisson Feb 1 '11 at 3:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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