在shell脚本中的grep模式中使用literal(*)

时间:2014-01-10 18:42:08

标签: bash grep

我正在尝试评估shell脚本中的grep表达式,并且grep使用文字星号(*),但该星号似乎是由我的bash扩展而不是保留文字星号:

branch_description=$(git branch --list -vv | grep "^\*")

如何在此上下文中运行grep并让它在PATTERN参数中接收文字星号?

3 个答案:

答案 0 :(得分:3)

解决方案是使用ascii八进制代码:

branch_description=$(git branch --list -vv | grep "^\052")

man 7 ascii

答案 1 :(得分:2)

问题是bash正在解释\并将其剥离,因为它在双引号内。改为

branch_description=$(git branch --list -vv | grep '^\*')

会做你想要的。请参阅bash手册中的QUOTING部分。

答案 2 :(得分:1)

您可以在grep中使用单引号来避免shell扩展*

branch_description=$(git branch --list -vv | grep '^\*')