检查字符串是否匹配

正则表达式

data1="aaab"
data2="aaaba"
data3="bbba"
pattern='a+b'

#[[ =~ ]] is true when the pattern is found anywhere in the text
[[ $data1 =~ $pattern ]] && b1=true || b1=false
#b1 is true

[[ $data2 =~ $pattern ]] && b2=true || b2=false
#b2 is true

[[ $data3 =~ $pattern ]] && b3=true || b3=false
#b3 is false

#grep -q prints nothing, the answer is its exit code
grep -qE "$pattern" <<< "$data3" && fromGrep=true || fromGrep=false
#fromGrep is false

echo "b1 is $b1"
echo "b2 is $b2"
echo "b3 is $b3"
echo "fromGrep is $fromGrep"