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:In the
ALIASES
section: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:
This will work for simple commands, but for more complex ones we need to tweak it.
The reason for the change is because of something like this:
As you can see no shell parsing is done. By changing it to use
sh -c "..."
instead, it does shell interpretation.Another way (I thought of this one first, so keeping it in the answer, but it's not as nice as the one above):
The
fc -e
command will run the specified command passing it a file containing the previously executed command. We just runsed
on that file to prefix the command withsudo
.