Creating an alias containing bash history expansion
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?
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)"'
From a colleague at work:
alias sa='sudo `history -p \!\!`'
appears to do the trick