我想使用正则表达式检查变量是否有有效的年份。阅读
bash manual我明白我可以使用operator =〜
看看下面的例子,我会看到“不OK”,但我看到“确定”。我究竟做错了什么?
i="test" if [ $i=~"200[78]" ] then echo "OK" else echo "not OK" fi
它在3.1和3.2之间改变:
This is a terse description of the new features added to bash-3.2 since the release of bash-3.1.
Quoting the string argument to the [[ command’s =~ operator now forces string matching,as with the other pattern-matching operators.
所以使用它不带引号,因此:
i="test" if [[ $i =~ 200[78] ]] ; then echo "OK" else echo "not OK" fi