转义 grep 中的双引号

我想用 grep 来处理包含双引号的关键字,举个简单的例子:

echo "member":"time" | grep -e "member\""

这个不匹配,我怎么才能修好它?

181445 次浏览

The problem is that you aren't correctly escaping the input string, try:

echo "\"member\":\"time\"" | grep -e "member\""

Alternatively, you can use unescaped double quotes within single quotes:

echo '"member":"time"' | grep -e 'member"'

It's a matter of preference which you find clearer, although the second approach prevents you from nesting your command within another set of single quotes (e.g. ssh 'cmd').