加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 为什么在[[…]]之间不执行引用删除?

发布时间:2020-12-15 18:57:53 所属栏目:安全 来源:网络整理
导读:$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
$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

命令1

$[[ "hello" =~ "he"   ]] && echo YES || echo NO
YES

命令2

$[[ "hello" =~  he.*  ]] && echo YES || echo NO
YES

命令3

$[[ "hello" =~ "he.*" ]] && echo YES || echo NO
NO

为什么命令2和3不同?

检查你的bash版本.从版本3.2开始,添加了以下状态:

Quoting the string argument to the [[ command’s =~ operator now forces
string matching,as with the other pattern-matching operators.

我猜你正在使用bash> = ver 3.2进行测试.

这就是你引用正则表达式的原因,它正在进行简单的字符串匹配而不是正则表达式匹配.

更新:如果你想在双引号内匹配正则表达式,那么使用:

shopt -s compat31

根据手册:

compat31

If set,bash changes its behavior to that of version 3.1
with respect to quoted arguments to the conditional command’s =~ operator.

这会导致您的命令行为不同:

[[ "hello" =~ "he.*" ]] && echo YES || echo NO
YES

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读