为什么在Bash with Double Brackets(" [[")?中,对于平等操作员的订单很重要(" ==")?

时间:2017-07-07 18:33:09

标签: bash

以下命令可以正常运行,打印出来"呵呵?":

remote origin
Fetch URL: their_package_repo.git
Push  URL: mypersonal_repo.git
HEAD branch: master
Remote branches:
develop                         tracked
master                          tracked
some_other_branch               tracked
Local branches configured for 'git pull':
  develop merges with remote develop
  master  merges with remote master
Local refs configured for 'git push':
  develop pushes to develop (fast-forwardable)
  master  pushes to master  (local out of date)

但如果我切换操作数的顺序,则不打印任何内容:

$ if [[ "odd behavior" == *odd* ]]; then echo "huh?"; fi
huh?

这对我来说似乎是违反直觉的。有人可以解释为什么行为不同吗?

1 个答案:

答案 0 :(得分:3)

正确处理正确的参数,它可以是通配符模式,但左参数不能。来自manual

  

当使用'=='和'!='运算符时,运算符右侧的字符串被视为模式并根据模式匹配中下面描述的规则进行匹配,就好像启用了extglob shell选项一样。 '='运算符与'=='相同。如果启用了nocasematch shell选项(请参阅Shopt Builtin中的shopt说明),则执行匹配而不考虑字母字符的情况。如果字符串匹配('==')或与模式不匹配('!='),则返回值为0,否则返回1。可以引用模式的任何部分以强制引用的部分匹配为字符串。