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?

qdii
  • 926

1 Answers1

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

  • Not sure how to mark this as a duplicate but linked to exact same question with an accepted answer. –  Jun 30 '13 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 Jun 30 '13 at 13:26
  • Added a bit more background on the bash man page versions to help give more context –  Jun 30 '13 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 Jun 30 '13 at 14:03
  • There's no flag under the question for me. Maybe I'm not cool enough for that (yet). –  Jun 30 '13 at 14:16
  • According http://serverfault.com/help/privileges you can flag posts with 15 rep. – dawud Jun 30 '13 at 14:50
  • Ah, found the "flag" text link, I was looking for a flag icon in the question area, doh! I tried to flag it as duplicate, but had trouble providing the right question id/link so I've given up. –  Jul 01 '13 at 02:55
  • @qdii As a general rule of thumb the application's own man page and official documentation have much more weight / authority / credibility than some external guide. TLDP does not have the best quality documentation for shell scripting unfortunately. If you want to get good solid information on bash scripting I'd check out the Wooledge wiki. – jw013 Jul 03 '13 at 15:07