当测试字符串包含通配符时,==
是否像 IN
运算符一样运行?非常神奇...
示例 1:
string='My long string';
if [[ $string == *"My long"* ]]; then echo "It's there!"; fi
输出:
It's There
例子2:
if [[ *"My long"* == $string ]]; then echo "It's there!"; fi
没有输出。
最佳答案
就是这样[[ ]]
专为工作而设计。它在手册中:
When the ‘==’ and ‘!=’ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below in Pattern Matching.
关于Bash:为什么比较优先级对通配符很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554386/