Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

migrated from serverfault.com Jul 3 '13 at 12:21

This question came from our site for system and network administrators.

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 Jun 30 '13 at 14:05
1  
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. –  Dennis Williamson Jul 1 '13 at 1:30

1 Answer 1

up vote 4 down vote accepted

Duplication Question (with answer)

http://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 http://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

share|improve this answer
    
Not sure how to mark this as a duplicate but linked to exact same question with an accepted answer. –  Drew Khoury 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 –  Drew Khoury 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). –  Drew Khoury Jun 30 '13 at 14:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.