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 "It contains " the else echo " does not include " Fi A = " HelloWorld " B = " Low " IF [[$ A $ B == * * ]] the then echo " contains " the else echo " does not include " Fi thisString = " . 1 2. 3 45 " # source string The searchString = ' . 1 2 " # string Case $ thisString in * "$searchString"*) echo "包含" ;; *) echo "不包含" ;; esac STRING_A=$1 STRING_B=$2 if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]];then ## is not substring. echo "包含" exit 0 else ## is substring. echo "不包含" exit 1 fi
reference:
Site stackoverflow and segmentfault .