spl*_*cer 7 regex string variables bash literals
为什么以下bash脚本只打印出来variable worked?
#! /bin/bash
foo=baaz
regex='ba{2}z'
if [[ $foo =~ 'ba{2}z' ]]; then
echo "literal worked"
fi
if [[ $foo =~ $regex ]]; then
echo "variable worked"
fi
Run Code Online (Sandbox Code Playgroud)
bash文档中是否有一些内容表明=~运算符只适用于变量而不是文字?此限制是否适用于任何其他运营商?
你不再需要bash正则表达式的引号:
#! /bin/bash
foo=baaz
regex='ba{2}z'
if [[ $foo =~ ba{2}z ]]; then
echo "literal worked"
fi
if [[ $foo =~ $regex ]]; then
echo "variable worked"
fi
# Should output literal worked, then variable worked
Run Code Online (Sandbox Code Playgroud)
我不记得哪个版本改变了这个.