shell 判断变量中是否包含某个字符串

如题所述

比如变量是str
str="this is a string"
要想在判断str中是否含有"this"这个字符串,下面的语句是可行的
[[ $str =~ "this" ]] && echo "\$str contains this"
[[ $str =~ "that" ]] || echo "\$str does NOT contain this"

其实这里就是用到了"[[" 判断命令和 "=~"正则式匹配符号
温馨提示:答案为网友推荐,仅供参考
相似回答