bash正则expression式与报价?

下面的代码

number=1 if [[ $number =~ [0-9] ]] then echo matched fi 

作品。 如果我尝试在正则expression式中使用引号,则会停止:

 number=1 if [[ $number =~ "[0-9]" ]] then echo matched fi 

我也尝试了"\[0-9\]" 。 我错过了什么?

有趣的是, bash先进的脚本指南build议这应该工作。

Bash版本3.2.39。

它在3.1和3.2之间改变了。 猜猜高级指南需要更新。

这是自bash-3.1发布以来添加到bash-3.2中的新特性的简要描述。 与往常一样,手册页(doc / bash.1)是查找完整描述的地方。

  1. Bash的新function

SNIP

F。 现在将string参数引用到[[command's =〜运算符]现在强制string匹配,与其他模式匹配运算符一样。

可悲的是,这将打破现有的报价使用脚本,除非你有洞察力存储模式的variables,并使用它们,而不是正则expression式直接。 下面的例子。

 $ bash --version GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. $ number=2 $ if [[ $number =~ "[0-9]" ]]; then echo match; fi $ if [[ $number =~ [0-9] ]]; then echo match; fi match $ re="[0-9]" $ if [[ $number =~ $re ]]; then echo MATCH; fi MATCH $ bash --version GNU bash, version 3.00.0(1)-release (i586-suse-linux) Copyright (C) 2004 Free Software Foundation, Inc. $ number=2 $ if [[ $number =~ "[0-9]" ]]; then echo match; fi match $ if [[ "$number" =~ [0-9] ]]; then echo match; fi match 

Bash 3.2引入了兼容性选项compat31,它将bash正则expression式引用行为恢复为3.1

没有compat31:

 $ shopt -u compat31 $ shopt compat31 compat31 off $ set -x $ if [[ "9" =~ "[0-9]" ]]; then echo match; else echo no match; fi + [[ 9 =~ \[0-9] ]] + echo no match no match 

与compat31:

 $ shopt -s compat31 + shopt -s compat31 $ if [[ "9" =~ "[0-9]" ]]; then echo match; else echo no match; fi + [[ 9 =~ [0-9] ]] + echo match match 

链接到补丁: http : //ftp.gnu.org/gnu/bash/bash-3.2-patches/bash32-039

GNU bash,版本4.2.25(1)-release(x86_64-pc-linux-gnu)

string匹配和正则expression式匹配的一些例子

  $ if [[ 234 =~ "[0-9]" ]]; then echo matches; fi # string match $ $ if [[ 234 =~ [0-9] ]]; then echo matches; fi # regex natch matches $ var="[0-9]" $ if [[ 234 =~ $var ]]; then echo matches; fi # regex match matches $ if [[ 234 =~ "$var" ]]; then echo matches; fi # string match after substituting $var as [0-9] $ if [[ 'rss$var919' =~ "$var" ]]; then echo matches; fi # string match after substituting $var as [0-9] $ if [[ 'rss$var919' =~ $var ]]; then echo matches; fi # regex match after substituting $var as [0-9] matches $ if [[ "rss\$var919" =~ "$var" ]]; then echo matches; fi # string match won't work $ if [[ "rss\\$var919" =~ "$var" ]]; then echo matches; fi # string match won't work $ if [[ "rss'$var'""919" =~ "$var" ]]; then echo matches; fi # $var is substituted on LHS & RHS and then string match happens matches $ if [[ 'rss$var919' =~ "\$var" ]]; then echo matches; fi # string match ! matches $ if [[ 'rss$var919' =~ "$var" ]]; then echo matches; fi # string match failed $ $ if [[ 'rss$var919' =~ '$var' ]]; then echo matches; fi # string match matches $ echo $var [0-9] $ $ if [[ abc123def =~ "[0-9]" ]]; then echo matches; fi $ if [[ abc123def =~ [0-9] ]]; then echo matches; fi matches $ if [[ 'rss$var919' =~ '$var' ]]; then echo matches; fi # string match due to single quotes on RHS $var matches $var matches $ if [[ 'rss$var919' =~ $var ]]; then echo matches; fi # Regex match matches $ if [[ 'rss$var' =~ $var ]]; then echo matches; fi # Above eg really is regex match and not string match $ $ if [[ 'rss$var919[0-9]' =~ "$var" ]]; then echo matches; fi # string match RHS substituted and then matched matches $ if [[ 'rss$var919' =~ "'$var'" ]]; then echo matches; fi # trying to string match '$var' fails $ if [[ '$var' =~ "'$var'" ]]; then echo matches; fi # string match still fails as single quotes are omitted on RHS $ if [[ \'$var\' =~ "'$var'" ]]; then echo matches; fi # this string match works as single quotes are included now on RHS matches 

正如其他答案中所提到的,将正则expression式放在一个variables中是通过不同的bash版本实现兼容性的一般方法。 您也可以使用此解决方法来实现同样的事情,同时保持您的正则expression式在条件expression式中:

 $ number=1 $ if [[ $number =~ $(echo "[0-9]") ]]; then echo matched; fi matched $