$ myfunc 'o "M' 'echo "My String"'String 'echo "My String"' contain substring 'o "M'.
$ myfunc 'alt' 'echo "My String"'String 'echo "My String"' don't contain substring 'alt'.
注意事项:您必须转义或双引号和/或双引号:
$ myfunc 'o "M' echo "My String"String 'echo My String' don't contain substring: 'o "M'.
$ myfunc 'o "M' echo \"My String\"String 'echo "My String"' contain substring: 'o "M'.
简单的功能
这是在BusyBox,Dash,当然还有Bash下测试的:
stringContain() { [ -z "${2##*$1*}" ]; }
那么现在:
$ if stringContain 'o "M3' 'echo "My String"';then echo yes;else echo no;fino$ if stringContain 'o "M' 'echo "My String"';then echo yes;else echo no;fiyes
/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'
每次更换测试:
[[ $b =~ $a ]] 2.92 user 0.06 system 0:02.99 elapsed 99% CPU
[ "${b/$a//}" = "$b" ] 3.16 user 0.07 system 0:03.25 elapsed 99% CPU
[[ $b == *$a* ]] 1.85 user 0.04 system 0:01.90 elapsed 99% CPU
case $b in *$a):;;esac 1.80 user 0.02 system 0:01.83 elapsed 99% CPU
doContain $a $b 4.27 user 0.11 system 0:04.41 elapsed 99%CPU
(在F. Houri的回答中包含)
至于傻笑:
echo $b|grep -q $a 12.68 user 30.86 system 3:42.40 elapsed 19% CPU !ouch!