字符串包含关系判断

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

 

posted @ 2019-01-11 20:42  道霖  阅读(896)  评论(0编辑  收藏  举报