新浪博客

shell字符串包含的5种判断方法

2023-01-05 16:10阅读:
shell字符串包含的5种判断方法:
方法一:
strA='long string'
strB='string'
result=$(echo $strA | grep '${strB}')
if [[ '$result' != '' ]]
then
echo '包含'
else
echo '不包含'
fi


方法二(亲测可以):
strA='helloworld'
strB='low'
if [[ $strA =~ $strB ]]
then
echo '包含'
else
echo '不包含'
fi


方法三:


A='helloworld'
B='low'
if [[ $A == *$B* ]]
then
echo '
包含'
else
echo '不包含'
fi


方法四:
thisString='1 2 3 4 5' # 源字符串
searchString='1 2' # 搜索字符串
case $thisString in
*'$searchString'*) echo '包含' ;;
*) echo '不包含' ;;
esac

我的更多文章

下载客户端阅读体验更佳

APP专享