1、字段 grep
案例:
str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep "${str2}")
if [[ "$result" != "" ]];then
    echo "包含"
else
    echo "不包含"
fi


2. 字符串运算符 =~:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 =~ $str2 ]];then
    echo "包含"
else
    echo "不包含"
fi


3、 正则表达式中的通配符 *:
案例:
str1="abcdefgh"
str2="def"
if [[ $str1 == *$str2* ]];then
    echo "包含"
else
    echo "不包含"
fi

 

 

欢迎加入QQ群一起讨论Linux、开源等技术

Shell脚本中判断字符串是否被包含在内

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
猜你喜欢
  • 2021-11-03
  • 2021-07-12
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案