在 shell 脚本中的 grep 模式中使用文字 (*) [英] Use literal (*) in grep pattern within shell script

查看:21
本文介绍了在 shell 脚本中的 grep 模式中使用文字 (*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to evaluate a grep expression inside of a shell script, and that grep uses a literal asterisk (*), but that asterisk appears to be expanded by my bash instead of remaining a literal asterisk:

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

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

What can I do to run grep in this context and let it receive a literal asterisk in its PATTERN argument?

推荐答案

问题在于 bash 正在解释 并将其剥离,因为它位于双引号内.更改为

The problem is that bash is interpreting the and stripping it away, because it's inside double quotes. Changing to

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

会做你想做的.请参阅 bash 手册中有关 QUOTING 的部分.

will do what you want. See the section on QUOTING in the bash manual.

这篇关于在 shell 脚本中的 grep 模式中使用文字 (*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆