find string including quotation marks using grep

754 views Asked by At

How can I find a string which includes quotation marks with grep? I tried a backslash to escape but this doesn't work.

For example search for the string "localStorage['api']". I tried:

grep -r "localStorage['api']" /path
grep -r "localStorage[\'api\']" /path
1

There are 1 answers

0
fedorqui On BEST ANSWER

Your escaping is OK. The problem lies in the [], that grep understands as regular expressions. Thus, you need to somehow tell it to treat the string as literal. For this we have -F:

grep -F "localStorage['api']" file

Test

$ cat a
hello localStorage['api'] and blabla
bye
$ grep -F "localStorage['api']" a
hello localStorage['api'] and blabla

From man grep:

-F, --fixed-strings

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.