为什么 $((40-35)) 变成了 5?

Joh*_*han 3 shell bash arithmetic

背后隐藏着什么奇怪的shell语法,它是$((40-35))如何计算值的?

例子:

$> echo $((40-35))
5
Run Code Online (Sandbox Code Playgroud)

bah*_*mat 15

bash手册页:

  ((expression))
          The expression is evaluated according  to  the  rules  described
          below  under ARITHMETIC EVALUATION.  If the value of the expres-
          sion is non-zero, the return status is 0; otherwise  the  return
          status is 1.  This is exactly equivalent to let "expression".
Run Code Online (Sandbox Code Playgroud)

并进一步:

  Arithmetic Expansion
    Arithmetic expansion allows the evaluation of an arithmetic expression and
    the substitution of the result.  The format for arithmetic expansion is:

          $((expression))
Run Code Online (Sandbox Code Playgroud)

任何放入双括号的东西都会成为一个bash可以求值的数学表达式。