Sketch
Sketch

Reputation: 81

grep help - search for this string: '$i['

I'm having trouble with using grep to search for the following string: $i[

I tried using this command:

grep -ir "$i\[" * > search.txt

I've added the \ to escape the square bracket. Do I also need to escape the dollar sign symbol?

I'm using macOS terminal - which I believe is a Unix environment.

Anyway, what I want is the exact match for the sequence of characters above - it is okay if this string is contained within some other string.

To make clear what I am trying to describe I want this: *$i[* (where the * means wildcard or any character or whitespace on either side of $i[

But, for whatever reason I don't get exact matches containing $i[

The returned search results seem to include only the dollar sign and not the letter i next to the square bracket.

Upvotes: 1

Views: 92

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

Since you are searching for a fixed string, not a regex, you can use -F option with grep, and make sure you use single quotes to avoid any variable expansion:

grep -iFr '$i[' * > search.txt

If you still want to use a regex pattern, you need to escape both [ (as it is used in regex to define the start of a bracket expression) and $ (it is used to define the end of string position):

grep -ir '\$i\[' * > search.txt

Upvotes: 2

Related Questions