How to Use Grep to Search for the String “-1”

gnugrep

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

Best Answer

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"
Related Question