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

前端之家收集整理的这篇文章主要介绍了Bash双方括号正则表达式匹配问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
考虑以下代码
$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中所做的.@H_301_4@

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

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

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

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

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

并进一步:@H_301_4@

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

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

猜你在找的Bash相关文章