Search code examples
bashshellmathoperator-keywordarithmetic-expressions

difference between $[a-b] and $((a-b)) in bash


I don’t know this operator $[] and couldn’t find something about it. However I know that next two codes give the same output

a=4
b=1
echo $[a-b] # => 3

and

a=4
b=1
echo $((a-b)) # => 3

So what is $[] operator for, and what’s the difference with $(()) ?

In my zsh shell prompt, when I open any of them and no close them, I have mathsubst written.


Solution

  • Reading man bash says that the old format $[expression] is deprecated and will be removed. Otherwise they should be equivalent.

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

              $((expression))
    

    The old format $[expression] is deprecated and will be removed in upcoming versions of bash.