bash [[ = ]] 行為

bash [[ = ]] 行為

man bash:

[[ 表達式 ]]
[...] 表達式由下面條件表達式下所述的主要元素組成。 [[ 和 ]] 之間的單字不進行分詞和路徑名擴展;
[...] 當使用 == 和 != 運算子時,運算子右側的字串被視為模式,並根據模式匹配下描述的規則進行匹配。

整個部分=沒有提及單一的情況。

條件式
[...]
string1 == string2
string1 = string2
如果字串相等則為真。 = 應與測試命令一起使用以確保 POSIX 一致性。

從這個描述我期望

[[ a = $cmpstring ]]

檢查相等的字串和

[[ a == $cmpstring ]]

檢查模式匹配。但事實並非如此:

> [[ a == ? ]]; echo $?
0
> [[ a = ? ]]; echo $?
0
> [[ a == "?" ]]; echo $?
1

我是否誤解了某些內容,或者 bash 手冊頁忘記提及=

答案1

===和裡面的時候一樣[[...]]。根據最新man頁面,在SHELL GRAMMAR>>Compound Commands[[ expression ]]

The = operator is equivalent to ==

再往下,在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).

bash info頁:

在此輸入影像描述

相關內容