我正在尝试评估shell脚本中的grep
表达式,并且grep使用文字星号(*),但该星号似乎是由我的bash扩展而不是保留文字星号:
branch_description=$(git branch --list -vv | grep "^\*")
如何在此上下文中运行grep
并让它在PATTERN
参数中接收文字星号?
答案 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 '^\*')