11

I've already removed and reinstalled grep on Ubuntu but still the same error persists.

Whatever grep` command I type I always see the following output:

grep: invalid option -- '='
Usage: grep [OPTION]... PATTERN [FILE]...
Try grep --help for more information.`

So even

grep -V

results in this error, or

grep --help

same thing!

/bin/grep has the same byte size as it does on my other identical hosts, where it works fine, so it's likely to be some environmental variable or local setting.

Any ideas?

2
  • 2
    I honestly cannot tell you why, but going in and out of the bash shell a couple of times, and it's working perfectly - I'd still be interested in ideas as to why this occurred. other commands remained unaffected.
    – user77887
    Jul 19, 2012 at 1:17
  • 1
    probably you had an alias like grep -=. I'd suggest you to mark Jeremy's answer as the solution to the problem. Jan 28, 2013 at 15:47

4 Answers 4

25

In case this isn't yet fixed, see if there is any file in the directory that starts with a '-'.

Reference: Stephane Chazelas' answer here [EDIT: updated old link to a new working reference]

Helped me.

2
  • That was it for me! Pretty much a problem for any command that has a glob in it I guess. Feb 6, 2015 at 1:16
  • 1
    the reference link doesn't work (site under construction)
    – Dror Cohen
    Dec 14, 2019 at 0:09
6

Sounds like you have an alias to grep, which is adding invalid arguments. Try the following:

type grep

this will tell you if the grep command is an alias or not. If it is, check your .profile, .bash_profile and .bashrc files for where this alias is defined.

2
  • thanks, yeah I tried this, using the command alias too. I am sure this was part of the answer as I noticed that there were no aliases defined on the box, whereas there should have been a few (as this box is identical to some other boxes which show their alias list correctly) I should add that the problem is no longer there, when I execute the command type grep I do get the correct alias of grep is aliased to `grep --color=auto' which of course is just allowing me to see things in colour for tired eyes :)
    – user77887
    Jul 19, 2012 at 3:57
  • 2
    So you're saying that it's working fine now? Did you change anything? Jul 19, 2012 at 4:39
6

I think you need to use:

grep -e "="
3
  • 4
    If the command grep --help gives the invalid option error, then this is not an answer.
    – gertvdijk
    Jan 28, 2013 at 12:52
  • 2
    This helped me, I ended up here trying to grep search a string that contained -> which gave error "grep invalid option -- >". I added -e to my command to tell grep that the next argument is a pattern and not an argument. Apr 25, 2016 at 22:17
  • 1
    Helped me as well. Same thing: I need to look for method invocation (->) very often but kept getting the error. The -e flag was the solution. Thanks for elaborating @zeros-and-ones Jan 11, 2023 at 9:51
4

its because there's a file in the directory starting with -

example 1:

touch ./-mike.txt

ls

-mike.txt

grep -i blah *

grep: invalid max count

example 2:

touch ./-sike.txt

grep -i blah *

grep: invalid option -- 'k'

Usage: grep [OPTION]... PATTERN [FILE]...

Try 'grep --help' for more information.

solution remove files starting with - , example:

rm ./-sike.txt
1
  • 5
    You can also type grep -i blah -- * The two dashes tell grep that the options are over now and everything after them is actually a filename. Many programs accept these two dashes to indicate end-of-options. This way you don't need to delete your files just because grep cannot handle them ;-)
    – PerlDuck
    Jul 31, 2018 at 17:13

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