Why does sudo ignore aliases?

amphibient

I am running Ubuntu 10.04 and I use upstart for daemon management. My enterprise application is run as a daemon and must be run as root because of various privileges. E.g.:

sudo start my-application-long-ID
sudo stop my-application-long-ID
etc

I would like to introduce an alias to abbreviate these commands as something like:

alias startapp='sudo start my-application-long-ID'

and run it as startapp and that works but I would prefer to not have sudo in the alias.

alias startapp='start my-application-long-ID'

does not when run using sudo startapp, returning sudo: startapp: command not found.

However, when I added the alias:

alias sudo='sudo '

sudo startapp now works but I am still curious why sudo ignores aliases.

Ramesh

I see the below information from here.

When using sudo, use alias expansion (otherwise sudo ignores your aliases)

alias sudo='sudo '

The reason why it doesn't work is explained here.

Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is checked by bash for an alias, ll is ignored. We can tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related