Bash:为什么比较优先级对通配符很重要?

标签 bash shell

当测试字符串包含通配符时,== 是否像 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/

相关文章:

python - 错误的命名链接搜索和替换

bash - 声明一个空数组并与bash中的空值区分开来

shell - 在每个文本文件的顶部和底部添加新行

c - 使用 nohup 或 setsid 逃避 alarm() 进程超时?

php - echo 在 8 字节输入上写入成功,但在其他输入上写入失败

regex - 在 bash 脚本中使用密码验证似乎让我在工作中感到困惑

linux - 为什么它不起作用?如果bash中的指令

string - 从字符串中删除所有出现的单词

shell - spring-boot 在 java 中向 actuatorshell 添加自定义命令

c# - 为特定于域的操作创建独立的控制台(shell)