(Note: This is not a duplicate question.)
On the command line, you can simply use sudo !!
to run the previous command with sudo. However, how would this be done if the previous command uses an alias?
Likewise, if I wanted to use an alias to run the previous command with sudo, I could use one of the following (or other) options in the .bashrc file:
alias please='sudo "$BASH" -c "$(history -p !!)"'
alias please='sudo $(fc -ln -1)'
alias please='eval "sudo $(fc -ln -1)"'
alias please='function _please(){ eval "sudo $(fc -ln -1)"; }; _please'
alias please='sudo sh -c '"'"'sudo sh -c $(fc -ln -1)'"'"''
alias please="sudo sh -c 'sudo sh -c $(fc -ln -1)'"
alias temp='sudo -s eval $(fc -ln -1)'
please () (
my_source=~
past_command=$(fc -ln -1)
sd ${past_command}
)
I have tried every single one of those, and others, in an attempt to get these to work with an alias in the preceding command. I have tried expanding them with shopt -s expand_aliases
, and I have tried using the alias sd='sudo '
trick. They all work fine for normal commands, but sudo never knows what my aliases are.
I need to keep all customization options within my home directory.
If possible, I would also prefer to use an option like sudo sh -c . . .
so pipes and redirects also receive the sudo power.
Edit: It has been found that using the alias sd='sudo '
trick will work with sd !!
on the command line, but it still won't work as an alias.
"\es":"\C-p\C-asudo "
This sets Alt-s to go to the previous line, go to the beginning of that line, and type "sudo ".secure_path
is out of the question?alias sudo='sudo '
worked for my aliased command. i have na aliasvi='vim'
if i runvi /etc/hosts
and then runsudo !!
it runs the previous aliased command with sudo.vi
is a system command... are you sure that you are not just runningvi
?alias nano='vim'
andalias sudo='sudo '
and the executednano /etc/hosts
and afterwards executedsudo !!
and it opened my hosts file withvim