简体   繁体   English

grep双引号vs单引号

[英]grep double quotes vs single quotes

I was surprised to find out that the single quote version works with regular expressions just the same. 我惊讶地发现单引号版本与正则表达式一样工作。 The only real difference I see now is that double quotes expand variables inside the regex pattern. 我现在看到的唯一真正的区别是双引号扩展了正则表达式模式中的变量。 Is there anything else I am missing? 还有什么我想念的吗?

The difference between single quotes and double quotes is a shell issue, not a grep issue. 单引号和双引号之间的区别是shell问题, 而不是 grep问题。 It is the shell that decides to do or not to do variable expansion before passing the arguments to grep . 在将参数传递给grep之前,shell决定是否进行变量扩展。 Because the last step in shell processing of arguments is quote removal, grep never even sees the quotes. 因为参数的shell处理的最后一步是引用删除,所以grep甚至从不看引号。

Variable expansion is not the only difference between single and double quotes. 变量扩展不是单引号和双引号之间的唯一区别。 The shell also does command substitution and arithmetic expansion inside double quotes. shell还在双引号内执行命令替换和算术扩展。 For example: 例如:

$ echo "$(date) and 2+2=$((2+2))"
Tue Aug  5 18:52:39 PDT 2014 and 2+2=4
$ echo '$(date) and 2+2=$((2+2))'
$(date) and 2+2=$((2+2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM