(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 () (
    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.

  • This is mostly to see how it can be done, as I've spent hours trying to figure this out. As of right now, I am settling for a keyboard shortcut in my .inputrc file with this line: "\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 ".
    – Ness
    Feb 21, 2020 at 15:58
  • Creating a script with the command, and add it to secure_path is out of the question? Feb 21, 2020 at 16:30
  • 1
    alias sudo='sudo ' worked for my aliased command. i have na alias vi='vim' if i run vi /etc/hosts and then run sudo !! it runs the previous aliased command with sudo.
    Feb 21, 2020 at 16:47
  • @BANJOSA Mmh, vi is a system command... are you sure that you are not just running vi? Feb 21, 2020 at 16:52
  • 1
    yes it but i've aliased it to vim. I've done the same experiment with alias nano='vim'and alias sudo='sudo 'and the executed nano /etc/hosts and afterwards executed sudo !! and it opened my hosts file with vim
    Feb 21, 2020 at 17:13

1 Answer 1


There is no definition of any alias inside (the shell started by) sudo.

In fact, it is a common security practice to remove environment variables and avoid aliases definition when starting sudo.

Let me walk you tru an example to explain:

Lets create a /bin executable (which only root could create) and define it as user executable:

# echo $'/bin/sh\necho "running $0 by $USER with $EUID"' >/bin/usercmd
# chmod a+x /bin/usercmd

That new command could be executed by the user with simply usercmd and by root with sudo usercmd

$ usercmd
running usercmd as isaac with 1027

$ sudo usercmd
running usercmd as root with 0

Both a user and root could run the executable.

And now lets define a very simple alias, and try it:

$ alias tcmd='usercmd'
$ tcmd
$ tcmd
running /usr/bin/usercmd as isaac with 1027

$ sudo tcmd
sudo: tcmd: command not found

There is no alias that sudo could use.

The only (general) solution is to extract the command inside the alias:

$ sudo sudo $(alias tcmd | sed 's/[^=]*=//;s/'\''//g')
running /usr/bin/usercmd as root with 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.