How to unalias a minus?

My morning coffee hadn't reached my brain yet when I forgot the quotes in:

alias grep="grep --color=always"

so instead I typed:

alias grep=grep --color=always

leaving me with:

--color=always
grep=grep

in my aliases.

When I try to cleanup the --color=always using unalias --color I get:

bash: unalias: --: invalid option
unalias: usage: unalias [-a] name [name ...]

I tried escaping, quoting and begging, but I can not figure out how to make the commando unalias cleanup the mess.

(I know a restart will do the trick but surely there must be another way)


Solution 1:

You can avoid POSIX compliant systems interpreting dashes in commands by adding -- before any arguments.

mtak@frisbee:~$ alias grep=grep --color=always
mtak@frisbee:~$ alias | grep color
alias --color='always'

mtak@frisbee:~$ unalias -- --color
mtak@frisbee:~$ alias | grep color
mtak@frisbee:~$

This also works with other utilities, let's say you have a file named -bla. If you try to remove it with rm you will get the following error:

mtak@frisbee:~$ ls -- -bla
-bla
mtak@frisbee:~$ rm -bla
rm: invalid option -- 'b'
Try 'rm ./-bla' to remove the file '-bla'.
Try 'rm --help' for more information.

By using -- before the filename, you will remove the file:

mtak@frisbee:~$ rm -- -bla
mtak@frisbee:~$ ls -- -bla
ls: cannot access '-bla': No such file or directory

Solution 2:

Just use the same trick you used to set the alias

unalias whatever --color