Answers:
[[
是bash保留字,因此应用特殊的扩展规则(例如算术扩展),而不是使用[
。还使用算术二进制运算符-eq
。因此,shell查找整数表达式,如果在第一项中找到文本,它将尝试将其扩展为参数。它称为算术扩展,存在于中man bash
。
RESERVED WORDS
Reserved words are words that have a special meaning to the shell.
The following words are recognized as reserved
…
[[ ]]
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of
the conditional expression expression. Expressions are
composed of the primaries described below under CONDITIONAL
EXPRESSIONS. Word splitting and pathname expansion are not
performed on the words between the [[ and ]]; tilde
expansion, parameter and variable expansion, >>>_arithmetic
expansion_<<<, command substitution, process substitution, and
quote removal are performed.
Arithmetic Expansion
…
The evaluation is performed according to the rules listed below
under ARITHMETIC EVALUATION.
ARITHMETIC EVALUATION
…
Within an expression, shell variables may also be referenced
by name without using the parameter expansion syntax.
因此,例如:
[[ hdjakshdka -eq fkshdfwuefy ]]
将始终返回true
但是这个会返回错误
$ [[ 1235hsdkjfh -eq 81749hfjsdkhf ]]
-bash: [[: 1235hsdkjfh: value too great for base (error token is "1235hsdkjfh")
还可以使用递归:
$ VALUE=VALUE ; [[ VALUE -eq 12 ]]
-bash: [[: VALUE: expression recursion level exceeded (error token is "VALUE")
man bash
为了清楚起见,我在答案中加入了引文。
[[
保留字的事实,而是因为里面的[[ … ]]
内容不是普通的命令语法,而是条件表达式。在条件表达式中,诸如的算术运算符的参数要-eq
经过算术评估。