Bash – Getting Sudo to Work for an Alias

aliasbashpathsudo

I installed Sublime Text 3 in Fedora 20 which has got GNOME, as I couldn't get it being called from terminal I created a .bash_aliases file which a line like this:

alias sublime='sublime_text'

And so also added the sublime_text file location to the PATH by editing .bashrc file adding:

export PATH=$PATH:/usr/local/share/applications/sublime-text-3/

However, when I want to edit any file for which I might get sudo privilege first I want to do:

sudo sublime sudoLikelyFile

or

sudo sublime_text sudoLikelyFile

However I'm getting:

sudo: sublime: command not found

How can I work around for this?

Best Answer

There are two potential problems here.

Firstly, you need to make your shell expand aliases after sudo:

alias sudo='sudo '

From man bash:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

Secondly, on many distributions, $PATH is not propagated to the new shell by sudo. You can manually change secure_path to add the directory which the sublime_text executable is in:

Defaults secure_path = [...]:/usr/local/share/applications/sublime-text-3

...or you can disable secure_path altogether (note: secure_path makes sure that an attacker cannot change your path and use it to influence the command you will run as root, so consider this carefully):

Defaults !secure_path
Related Question