82

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)

  • 13
    Ha ha, now I got it stuck in mine also, working on it EDIT: Looks like choroba's answer worked for me – AJ Smith 'Smugger' Mar 2 '17 at 10:05
  • 9
    Kind of reminds me of the old echo > \\?\c:\windows\x., or that time I rm -rf * ~ by accident. – Jason C Mar 3 '17 at 1:08
124

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
| improve this answer | |
  • 19
    Though it's worth noting that for files, an equally valid method is to use a path to the file that avoids this; so you can use ls ./-bla and rm ./-bla (or specify the full paths, whatever's easier in your case). Obviously this doesn't work for aliases though! – Muzer Mar 2 '17 at 11:52
  • 1
    pubs.opengroup.org/onlinepubs/009695399/basedefs/… Guideline 10 – user115383 Mar 5 '17 at 1:20
91

Just use the same trick you used to set the alias

unalias whatever --color
| improve this answer | |
  • 9
    works indeed, +1 but the other solution is a bit cleaner :) – Requist Mar 2 '17 at 10:16
  • 1
    Indeed, of course :-) – choroba Mar 2 '17 at 10:19
  • unalias unalias --color – Cœur Mar 3 '17 at 10:40
3

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 and grep; 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 from man 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 for alias by your shell; to avoid this you need communicate to your bash shell that what is following it is not an option.

  • The Holy Grail of Linux: man, in this case man bash that is a coffer filled with treasures useful in situation like this.
    You can read from man 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 for alias even for unalias 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 and unalias.

    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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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