bash regex with quotes?

后端 未结 4 2089
走了就别回头了
走了就别回头了 2020-11-22 12:53

The following code

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

works. If I try to use quotes in the regex, however, it stops:

4条回答
  •  长情又很酷
    2020-11-22 13:33

    As mentioned in other answers, putting the regular expression in a variable is a general way to achieve compatibility over different bash versions. You may also use this workaround to achieve the same thing, while keeping your regular expression within the conditional expression:

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

提交回复
热议问题