bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))

Chen Levy picture Chen Levy · Mar 10, 2010 · Viewed 6.9k times · Source

I have just stumbled upon the bash syntax:

foo=42
bar=$[foo+1] # evaluates an arithmetic expression

When I Googled for this, I found http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05:

3.4.6. Arithmetic expansion

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

$(( EXPRESSION )) 

...

Wherever possible, Bash users should try to use the syntax with square brackets:

$[ EXPRESSION ] 

However, this will only calculate the result of EXPRESSION, and do no tests...

In my bash man page I can only find the $(( EXPRESSION )) form such as:

foo=42
bar=$((foo+1)) # evaluates an arithmetic expression

So what tests are not performed with $[...] that do with $((...)), or is the $[...] just a legacy version of $((...))?

Answer

sth picture sth · Mar 10, 2010

The manpage for bash v3.2.48 says:

[...] The format for arithmetic expansion is:

     $((expression))

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

So $[...] is old syntax that should not be used anymore.