考虑以下代码:
$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之前,您提供的示例可以按预期工作.