为什么 shell 解释单引号内的字符?
我正在使用 bash shell
$cat test
a --b
$grep '--b' test
grep: option `--b' is ambiguous
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$grep "--b" test
grep: option `--b' is ambiguous
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$grep '\-\-b' test
a --b
经典的 shell 脚本书籍第 7 章“输入和输出、文件和命令评估”第 7.7 节引用说
“单引号强制 shell 按字面意思处理一对引号之间的所有内容”。
那么为什么上面的单引号不起作用呢?
I am using bash shell
$cat test
a --b
$grep '--b' test
grep: option `--b' is ambiguous
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$grep "--b" test
grep: option `--b' is ambiguous
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$grep '\-\-b' test
a --b
The classic shell scripting book in chapter 7 "Input and Output,Files, and Command Evaluation" in section 7.7 Quoting says that
"Single quotes force the shell to treat everything between the pair of quotes literally".
Then why is single quotes not working above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
shell按字面意思对待它们。但 shell 不负责应用程序如何对待它们。由应用程序决定如何处理作为参数传递的
--b
。The shell is treating them literally. But the shell is not responsible for how the application treats them. It is up to the application to decide how it wants to handle the
--b
passed as an argument.大多数 gnu 实用程序都会忽略
--
之后的--options
,因此您可能需要执行grep -- --b test
。既然您标记了您的问题linux
,那么您很可能拥有 gnu grep。Most of gnu utilities ignore
--options
after--
, so you may want to do agrep -- --b test
instead. And since you tag your questionlinux
, chances are you have gnu grep.