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 ?


  • It seems that just escaping the "-" will work: grep -irn "\-1" – schrodigerscatcuriosity 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

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"
| 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.