为什么不是[[...]]之间进行引用删除? [英] why quote removal isn't performed between [[ ... ]]?

查看:93
本文介绍了为什么不是[[...]]之间进行引用删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ 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屋!

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