5

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?

2

2 Answers 2

11

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.

1
  • 1
    Nice idea with the alias! I don't like disabling it though. One variation is to keep it and still use the reminder, e.g.: alias rm='echo "tip: use trash next time"; rm'.
    – Nagev
    Feb 15, 2021 at 22:50
1

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.

1
  • That doesn't seem to work with either bash or zsh. Sep 25, 2010 at 20:10

You must log in to answer this question.

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