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

Bash双方括号正则表达式匹配问题

发布时间:2020-12-16 01:20:02 所属栏目:安全 来源:网络整理
导读:考虑以下代码: $var1=bingo$var2=.ingo$if [[ "$var1" =~ $var2 ]]; then echo found; fifound$if [[ $var1 =~ "$var2" ]]; then echo found; fi # line 5$if [[ "$var1" =~ "$var2" ]]; then echo found; fi # line 6$if [[ $var1 =~ $var2 ]]; then echo
考虑以下代码:
$var1=bingo
$var2=.ingo
$if [[ "$var1" =~ $var2 ]]; then echo found; fi
found
$if [[ $var1 =~ "$var2" ]]; then echo found; fi    # line 5
$if [[ "$var1" =~ "$var2" ]]; then echo found; fi  # line 6
$if [[ $var1 =~ $var2 ]]; then echo found; fi
found

以上就是我在bash shell中所做的.

问题是为什么没有找到第5和第6行的打印件?

我想我已经知道了答案,但我正在寻找一个简单易懂的答案.

总而言之,当在=?的右侧使用变量(内部双引号)时,双引号是否仅用于变量扩展?

假设您正在运行Bash 3.2或更新版本,则 bash manual(向下滚动到[[…]]的描述)指出:

Any part of the pattern may be quoted to force the quoted portion to be matched as a string.

并进一步:

If the pattern is stored in a shell variable,quoting the variable expansion forces the entire pattern to be matched as a string.

在Bash 3.2之前,您提供的示例可以按预期工作.

(编辑:李大同)

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

    推荐文章
      热点阅读