Barton Chittenden
Barton Chittenden

Reputation: 4416

bash: grepping for double quotes is failing

I'm trying to grep stdin for a string that looks like this:

"\t", 7

The number '7' is a variable, so I want to do something like this:

mumble | grep "\"\\t\", $number"

The grep isn't returning anything. I tried to decompose this in to its simplest terms:

$ echo '"\t"'
"\t"

... as expected.

$ echo "\"\\t\""
"\t"

This renders the string that I want when interpolated in double quotes. But...

$ echo '"\t"' | grep "\"\\t\""

doesn't return anything.

Ok, let's turn on set -x to see what bash thinks is going on...

$ echo '"\t"' | grep "\"\\t\""
+ grep '"\t"'
+ echo '"\t"'

... so I'm grepping for exactly what's being echoed... and single quotes aren't special characters in basic regex, so why isn't grep matching this?

Upvotes: 1

Views: 1869

Answers (2)

Barton Chittenden
Barton Chittenden

Reputation: 4416

DoH!

'\' is a special character in regex...

$ echo -n '"\t"' | grep "\"\\\\t\""
+ grep '"\\t"'
+ echo -n '"\t"'
"\t"

matches.

Upvotes: 1

shyam
shyam

Reputation: 9368

echo ' asdfdf "\t",7 asdfadsf' | grep '"\\t",7'

Upvotes: 2

Related Questions