【发布时间】:2023-03-30 15:28:01
【问题描述】:
我正在尝试检查给定字符串中是否包含.rel6.。我对 Bash 正则表达式的行为有点困惑。我在这里错过了什么?
os=$(uname -r) # set to string "2.6.32-504.23.4.el6.x86_64"
[[ $os =~ *el6* ]] && echo yes # doesn't match, I understand it is Bash is treating it as a glob expression
[[ $os =~ el6 ]] && echo yes # matches
[[ $os =~ .el6 ]] && echo yes # matches
[[ $os =~ .el6. ]] && echo yes # matches
[[ $os =~ ".el6." ]] && echo yes # matches
[[ $os =~ *".el6." ]] && echo yes # * does not match - why? *
[[ $os =~ ".el6."* ]] && echo yes # matches
re='\.el6\.'
[[ $os =~ $re ]] && echo yes # matches
特别是这个:
[[ $os =~ *".el6." ]] && echo yes
【问题讨论】:
-
如果要检查
.el6.是否在字符串中,请使用[[ $os = *".el6."* ]] && echo yes。在这里,glob 模式将是*.el6.*,您需要在这里使用=运算符。