bash [[ = ]] 行为

Hau*_*ing 4 bash

man bash

[[ 表达式 ]]
[...] 表达式由以下条件表达式中描述的主要元素组成。对[[和]]之间的词不进行分词和路径名扩展;
[...] 当使用 == 和 != 运算符时,运算符右侧的字符串被视为一个模式,并根据下面模式匹配中描述的规则进行匹配。

在整个部分中,=没有提到单个的情况。

条件表达式
[...]
string1 == string2
string1 = string2
如果字符串相等则为真。= 应与测试命令一起用于 POSIX 一致性。

从这个描述中,我希望

[[ a = $cmpstring ]]
Run Code Online (Sandbox Code Playgroud)

检查相等的字符串和

[[ a == $cmpstring ]]
Run Code Online (Sandbox Code Playgroud)

检查模式匹配。但事实并非如此:

> [[ a == ? ]]; echo $?
0
> [[ a = ? ]]; echo $?
0
> [[ a == "?" ]]; echo $?
1
Run Code Online (Sandbox Code Playgroud)

我是误解了什么还是 bash 手册页刚刚忘记提及=

don*_*sti 7

===在里面的时候一样[[...]]。按更近的man页面,下SHELL GRAMMAR> Compound Commands> [[ expression ]]

The = operator is equivalent to ==
Run Code Online (Sandbox Code Playgroud)

再往下,在CONDITIONAL EXPRESSIONS

string1 == string2
string1 = string2
        True  if  the  strings  are equal.  = should be used with the test command
        for POSIX conformance. When used with the [[ command, this performs pattern
        matching as described above (Compound Commands).
Run Code Online (Sandbox Code Playgroud)

bash info 页:

在此处输入图片说明

  • “手册”页面的屏幕截图?真的吗? (5认同)