Creating an alias containing bash history expansion

aliasbashbash-aliassudo

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?

Best Answer

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)"'
Related Question