Bash – Why does !! inside an alias not work

aliasbashcommand historyhistory-expansion

I have this alias set in my system /etc/bashrc file:

alias root="sudo !!"

The intention of this being to run the last used command using sudo of course. When used, it of course appears to substitute the last command in history to the bashrc file upon shell initialization, and not the actual command that you would get if you were to run sudo !! in an interactive shell. I've also tried alias root="sudo fc -s" to no avail.

I realize this is probably something to do with how the BASH does command substitutions, but can someone explain why this is, and provide a usable substitute?

I'm running BASH version 3.2.51(1)-release (x86_64-apple-darwin13).

Best Answer

The key part in this behavior is explained by 2 bits in the bash manpage:

In the HISTORY EXPANSION section:

History expansion is performed immediately after a complete line is read, before the shell breaks it into words.

In the ALIASES section:

The first word of each simple command, if unquoted, is checked to see if it has an alias.

So basically history expansion occurs before word splitting. Alias expansion occurs after.


Alternate solution

The best way I can think of doing this is the following:

alias root='sudo $(fc -ln -1)'

This will work for simple commands, but for more complex ones we need to tweak it.

alias root='sudo sh -c "$(fc -ln -1)"'

The reason for the change is because of something like this:

# alias root='sudo $(fc -ln -1)'
# echo "I AM $(whoami)"
I AM patrick
# root
"I AM $(whoami)"

As you can see no shell parsing is done. By changing it to use sh -c "..." instead, it does shell interpretation.

# alias root='sudo sh -c "$(fc -ln -1)"'
# echo "I AM $(whoami)"
I AM patrick
# root
I AM root

Another way (I thought of this one first, so keeping it in the answer, but it's not as nice as the one above):

alias root='fc -e "sed -i -e \"s/^/sudo /\""'

The fc -e command will run the specified command passing it a file containing the previously executed command. We just run sed on that file to prefix the command with sudo.

Related Question