1. 程式人生 > >shell 判斷變數中是否包含某個字串

shell 判斷變數中是否包含某個字串

str="this is a string"
[[ $str =~ "this" ]] && echo "$str contains this" 
[[ $str =~ "that" ]] || echo "$str does NOT contain that"
結果為:
this is a string contains this
this is a string does NOT contains that
[[" 判斷命令和 "=~"正則式匹配符號