Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
1  
You don't specify if you are on OS X, but if so, check out the osx-trash command. Very useful: dribin.org/dave/osx-trash 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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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