字符串包含关系判断

1、利用grep查找

str1="ABC"
str2="A"

result=$(echo $str1 | grep "${str2}")

if [[ "$result" != "" ]];then
    echo "包含"
else
    echo "不包含"
fi

2、利用字符串运算符

str1="hellow"
str2="low"

if [[ $str1 =~ $str2 ]];then
    echo "包含"
else
    echo "不包含"
fi

3、利用通配符

str1="hellow"
str2="low"

if [[ $str1 == *$str2* ]];then
    echo "包含"
else
    echo "不包含"
fi

猜你喜欢

转载自www.cnblogs.com/t-road/p/10257202.html