3

I'm using grep in bash to search for the string "-1" .

I would usually search for "bar" like this ...

glaucon@foo $ grep -irn "bar"

... which works fine but when I try to search for "-1" I get this ...

glaucon@foo $ grep -irn "-1"
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

... I thought the minus might be a special character but it appears not . I also tried using the 'fixed strings' option in case this was some aspect of regex I was unfamiliar with ...

glaucon@foo $ grep -irnF "-1"
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

... but, as shown, that doesn't provide output either.

I'm sure this is straightforward but ... how ?

Thanks

glaucon
  • 133

1 Answers1

5

An option you have is to escape the dash - with a backslash \:

grep -irn "\-1"

Adding two dashes to mark the end of options (as suggested by @steeldriver):

grep -irn -- "-1"

Or, use -e to explicitly say "the next argument is the pattern":

grep -irn -e "-1"
Kusalananda
  • 333,661