为什么不是[[...]]之间进行引用删除? [英] why quote removal isn't performed between [[ ... ]]?
问题描述
$ bash的人
词的拆分和文件名扩展是不是[[和]]之间的单词进行;波浪线扩展,参数和变量扩展,算术扩展,命令替换,函数替换和引用的去除执行。
块引用>$回声$ BASH_VERSION
4.2.10(1)-release命令1
$ [你好=〜他]]&放大器;&安培;回声是||回声NO
是命令2
$ [你好=〜他*]]&功放;&安培;回声是||回声NO
是命令3
$ [你好=〜]他。*]放;&安培;回声是||回声NO
没有为什么命令2和3是不同的?
解决方案检查您的bash的版本。起价3.2版本中添加这种行为状态:
引用的字符串参数的[[命令的=〜运营商现在的力量
串匹配,与其它模式匹配运算
块引用>我猜你正在使用bash> = 3.2版本为您的测试。
这就是原因,当你引用的常规前pression它做朴素简单的字符串,而不是正则表达式匹配匹配。
更新:如果您想正则表达式匹配里面的双引号,然后使用:
禁用了javascript -s compat31
由于每本手册:
compat31
如果设置时,bash改变其行为到3.1版本
对于引用参数的条件命令的=〜运营商。
块引用>这将导致您的命令,以不同的表现:
[你好=〜他*。]]&放大器;&安培;回声是||回声NO
是$ man bash
Word splitting and filename expansion are not performed on the words between the ‘[[’ and ‘]]’; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.
$ echo $BASH_VERSION 4.2.10(1)-release
command 1
$ [[ "hello" =~ "he" ]] && echo YES || echo NO YES
command 2
$ [[ "hello" =~ he.* ]] && echo YES || echo NO YES
command 3
$ [[ "hello" =~ "he.*" ]] && echo YES || echo NO NO
Why command 2 and 3 are different?
解决方案Check your bash version. Starting from version 3.2 this behavior was added that states:
Quoting the string argument to the [[ command's =~ operator now forces string matching, as with the other pattern-matching operators.
I guess you are using bash >= ver 3.2 for your test.
That's the reason when you quote the regular expression it is doing plain simple string matching instead of regex matching.
Update: If you want regex matching inside double quotes then use:
shopt -s compat31
As per the manual:
compat31
If set, bash changes its behavior to that of version 3.1 with respect to quoted arguments to the conditional command's =~ operator.
which causes your command to behave differently:
[[ "hello" =~ "he.*" ]] && echo YES || echo NO YES
这篇关于为什么不是[[...]]之间进行引用删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!