Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
You don't specify if you are on OS X, but if so, check out the osx-trash command. Very useful: But as others have said, don't alias rm. Just get in the habit of typing "trash" instead. – user31752 Sep 25 '10 at 20:18

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.

share|improve this answer

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.

share|improve this answer
That doesn't seem to work with either bash or zsh. – Ryan Thompson Sep 25 '10 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .