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
  • 3
  • Related: [How to hand over arguments containing “-” in Bash?](https://unix.stackexchange.com/questions/523893/how-to-hand-over-arguments-containing-in-bash) – steeldriver Sep 12 '19 at 23:10
  • It seems that just escaping the "-" will work: `grep -irn "\-1"` – schrodingerscatcuriosity Sep 12 '19 at 23:19
  • @guillermochamorro - thanks. I thought I had tried that but it turns out that I hadn't, that works. As you were first do you want to make this an answer I can mark as 'correct' ? – glaucon Sep 12 '19 at 23:41
  • @steeldriver thanks for the link as a result of which I now know that this will also work : grep -irn -e "-1" . – glaucon Sep 12 '19 at 23:41
  • 2
    @glaucon yes that's what I'd probably use in the special case of `grep`; for GNU utilities more generally, the `--` end-of-options placeholder is my go-to – steeldriver Sep 12 '19 at 23:44

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
  • 320,670
  • 36
  • 633
  • 936
schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57