Unable to unalias rm and let it to be a function in Zsh
-
03-07-2019 - |
Question
I run unsuccessfully in my .zshrc
unalias rm
rm() { mv $* /tmp/wastebasket }
I get at the startup
/Users/Masi/.zshrc:unalias:34: no such hash table element: rm
I noticed that the hash table problem has been an unresolved bug in Ubuntu for run-help.
I am not sure whether the bug applies to Mac and to rm
-command too.
How can you get the notification off at the startup?
Solution
Everyone else is right that you simply didn't have an alias. More importantly:
DON'T do this. Some day you will be at another POSIX machine that follows POSIX standards (deleting without "recycling"), and you will casually delete something and have no way to undo it. Learn rm discipline now.
OTHER TIPS
That error message is because you're trying to unalias rm
and there is no such alias.
Since you can alias something more than once without an error, I would change your code to be:
alias rm=x
unalias rm
rm() { mv $* /tmp/wastebasket }
That guarantees that rm
exists as an alias before you try to unalias it.
I'm not very familiar with zsh, but perhaps it is because rm is not an alias, but is actually a standard utility residing in /bin.
You could just alias it without attempting to unalias it first, overriding any previous alias.
You should only try to remove an alias that exists. Creating an alias that is unaliased right away strikes me as ugly. My recommendation is to test for rm being an alias and unaliasing if so.
case $(type rm) in
(*alias*) unalias rm;;
esac
Or use brute force and ignore stderr with
unalias rm 2>/dev/null