shell 判断字符串是否包含
- =~ 判断字符串是否被包含(区分大小写)
v='Hello World' [[ $v =~ 'hello' ]] && echo 'Y' || echo 'N' [[ $v =~ 'Hello' ]] && echo 'Y' || echo 'N' [[ $v =~ 'World' ]] && echo 'Y' || echo 'N' [[ $v =~ 'test' ]] && echo 'Y' || echo 'N' # N # Y # Y # N
- 正则通配符 *
v='Hello World' [[ $v == *'hello'* ]] && echo 'Y' || echo 'N' [[ $v == *'Hello'* ]] && echo 'Y' || echo 'N' [[ $v == *'World'* ]] && echo 'Y' || echo 'N' [[ $v == *'test'* ]] && echo 'Y' || echo 'N' # N # Y # Y # N