How can I alias a command for sudo?

I have an alias that I would like to use both as a regular user and as root, via sudo. Specifically, it is this:

alias rm=trash

This works fine as a normal user, and it works fine when I use sudo -i to get a root shell prompt, but if I use sudo rm, the alias does not apply. So where do I need to put my alias so that it works in one-off sudo commands?


Create an alias for sudo with a space at the end:

alias sudo='sudo '

Then all other aliases will work with sudo.

But like mentioned in the other answer, aliasing rm to execute trash is probably a bad idea. What I like to do is "disable" rm like this:

alias rm='echo "rm is disabled, use trash or /bin/rm instead."'

With this you will soon get into the habit of typing trash instead of rm.


That won't work, but if you use a function and export it, it will.

dl () { trash "$@"; }
export -f dl

It's a bad idea to alias rm because if you get in the habit of the alias or function protecting you and at some point the alias isn't available for some reason then you use rm and it deletes a bunch of stuff that will be difficult to get back.