4

On the paragraph explaining arithmetic expansion, Bash's user guide uncovers 2 different ways of evaluating an expression, the first one uses $(( EXPRESSION )) and the second one uses $[ EXPRESSION ]. The two ways seem pretty similar as the only difference I have found is:

$[ EXPRESSION ] will only calculate the result of EXPRESSION, and do no tests:

Yet, I am intrigued because the same document recommends using $[ EXPRESSION ] rather than $(( EXPRESSION )).

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

Why would you want that if less tests are being done?

2
  • 2
    Not to be picky, but did you realize the TLDP doc you linked is from the past? why not check the most authoritative source of information for bash?
    – dawud
    Commented Jun 30, 2013 at 14:05
  • 2
    Additionally, the TLDP information is generally considered to be low quality. For good quality information in addition to the authoritative documentation see the pages at Greg Wooledge's Wiki. Commented Jul 1, 2013 at 1:30

1 Answer 1

5

Duplication Question (with answer)

https://stackoverflow.com/questions/2415724/bash-arithmetic-expression-vs-arithmetic-expression

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

In addition to that answer:

http://manual.cream.org/index.cgi/bash.1#27

Info relating to bash versions:

Here is some info about bash man pages (its hard to find info on what version each one is referring to):

OPs link:

http://www.tldp.org/guides.html Bash Guide for Beginners version: 1.11 author: Machtelt Garrels, last update: Dec 2008

sth (74.6k rep) quoting bash v3.2.48

from https://stackoverflow.com/questions/2415724/bash-arithmetic-expression-vs-arithmetic-expression)

Note: More info about [] vs (()) here: http://lists.gnu.org/archive/html/bug-bash/2012-04/msg00033.html

a link I found:

http://www.gnu.org/software/bash/manual/ last updated August 22, 2012

http://www.gnu.org/software/bash/manual/bash.html#Arithmetic-Expansion

8
  • Not sure how to mark this as a duplicate but linked to exact same question with an accepted answer.
    – Drew Khoury
    Commented Jun 30, 2013 at 12:50
  • to be quite honest, I am not sure this answers the question. Basically the linked bash documentation says exactly the opposite: use $(()) instead of $[] giving the reason of the former being deprecated. So I still don’t know why my link recommended using the latter, and I am unsure which documentation is more trustworthy.
    – qdii
    Commented Jun 30, 2013 at 13:26
  • Added a bit more background on the bash man page versions to help give more context
    – Drew Khoury
    Commented Jun 30, 2013 at 13:56
  • @DrewKhoury See the flag link underneath the OP question? Click on it. Afterwards, select it doesn't belong here, or it is a duplicate, use the checkbox that says duplicate of..., and then you will be able to provide the link to the question this one is a duplicate of.
    – dawud
    Commented Jun 30, 2013 at 14:03
  • There's no flag under the question for me. Maybe I'm not cool enough for that (yet).
    – Drew Khoury
    Commented Jun 30, 2013 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .