str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep "${str2}")
if [[ "$result" != "" ]];then
echo "包含"
else
echo "不包含"
fi
如果精確的匹配到def呢
其實答案很簡單,用grep –w "def"
或者是grep "\<def\>"
都可以實現
-w, --word-regexp 強制 PATTERN 僅完全匹配字詞
str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep -w "${str2}")
if [[ "$result" != "" ]];then
echo "包含"
else
echo "不包含"
fi