How to unalias a minus?
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
Just use the same trick you used to set the alias
unalias whatever --color
Fast solution
To fix both the wrong aliases you may simply run
unalias grep --color
Understanding what's happened
You give the wrong command alias grep=grep --color=always
without "..."
.
You set two aliases
--color
andgrep
; indeed you can read it on the left side of your output (all at the left of the=
is a aliased command):--color=always grep=grep
so you need to unset both.
Why this happened?
The...
in the synoptic of alias means that is possible to do multiple assignation in one line:alias: alias [-p] [name[=value] ... ]
In the output of
help alias
you can read the synoptic but fromman bash
you can read that is possible in the same line even to list and assign at the same time:When arguments are supplied, an alias is defined for each name whose value is given. A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded. For each name in the argument list for which no value is supplied, the name and value of the alias is printed...
You have problems to unset the first one
--color
alone.
This because it is interpreted as options foralias
by your shell; to avoid this you need communicate to yourbash
shell that what is following it is not an option.The Holy Grail of Linux:
man
, in this caseman bash
that is a coffer filled with treasures useful in situation like this.
You can read fromman bash
:-- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
As reported in other answer the POSIX way is to use the special character combination
--
.unalias -- --color
But why
unalias grep --color
is working? I mean without using--
?
As it worked foralias
even forunalias
it is possible to give more than one name in one time (again the[...]
).unalias: unalias [-a] name [name ...]
I can give only my guess: a good programming habit is to write the reading and writing routines at the same time, below the same logic. So it has to be for the option parsing routines of
alias
andunalias
.That this works you can see even from this command (from a shell opened for this purpose) that will not erase all your aliases:
unalias whatever -a
Final remark
In the BUGS section of man bash
among the few things reported you may read :-)
Aliases are confusing in some uses.