Korn shell interprets the value in side of $[ ] to be a variable name. It was added so that you can surround a variable name with other text and be sure that your variable will be expanded properly.<div><br></div><div>echo some$[variable_name]text </div>
<div><br></div><div>The POSIX shell standard was based on ksh88 so the function of $[ ] was already there. I would guess that when they decided to make bash compliant with the POSIX shell standard or add the feature to the POSIX spec they had to add $(( experssion )) and left $[ expression ] in bash for backwards compatibility.</div>
<div><br></div><div>In short always use $(( expression )) as it will work in more shells.</div><div><br></div><div><br><div class="gmail_quote">On Thu, Apr 12, 2012 at 12:11 PM, David L. Willson <span dir="ltr"><<a href="mailto:DLWillson@thegeek.nu" target="_blank">DLWillson@thegeek.nu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Aaron,<br>
<br>
Thanks for the well-thought-out answer. I'll return to using '$(( EXPRESSION ))', even though I can depend on bash for all my work.<br>
<br>
I guess that '$[ EXPRESSION ]' has some capabilities over the old style, but I never do anything terribly complex.<br>
<br>
In my specific application, getting some random numbers, '$(( EXPRESSION ))' is working perfectly.<br>
<br>
Case in point:<br>
<br>
# Generate a bunch of random looking numbers, in the range 1 to 10, inclusive.<br>
while true ; do echo -n "$(( RANDOM % 10 + 1 )) " ; done<br>
<div><br>
--<br>
David L. Willson<br>
Trainer, Engineer, Enthusiast<br>
RHCE Network+ A+ Linux+ LPIC-1 Ubuntu<br>
Mobile 720-333-LANS(5267)<br>
<br>
This is a good time for a r3volution.<br>
<br>
</div><div><div>----- Original Message -----<br>
> David L. Willson writes:<br>
> > Why are the square brackets preferred (see below)? Why are there<br>
> > two<br>
> > forms?<br>
><br>
> I probably don't have this completely correct, but... I'd say the<br>
> recommendation is incorrect.<br>
><br>
> It is my understanding that the $(( ... )) syntax is a POSIX shell<br>
> feature, while the $[ ... ] is something that bash added in the<br>
> early-mid 1990s. Given that, I try to use the $(( ... )) syntax<br>
> exclusively. The $[ .. ] syntax generally doesn't work on non-bash<br>
> Bourne shells. This may or may not be a concern for you.<br>
><br>
> Please stand by while I fire up the HP-UX box...<br>
><br>
> Here's what happens with $[ .. ] on a non-bash Bourne style shell:<br>
><br>
> $ SHELL=/usr/bin/sh; exec $SHELL -o vi<br>
> $ uname -a<br>
> HP-UX billandd B.11.23 U 9000/800 1822910606 unlimited-user<br>
> license<br>
> $ echo $((2 * 5))<br>
> 10<br>
> $ echo $[2 * 5]<br>
> $[2 fileset-cleanup 5]<br>
> $ exit<br>
><br>
> Compare with the results on a machine using bash:<br>
><br>
> adj@sacredchao:~$ echo $SHELL<br>
> /bin/bash<br>
> adj@sacredchao:~$ uname -a<br>
> Linux sacredchao 3.2.0-0.bpo.1-amd64 #1 SMP Sat Feb 11 08:41:32<br>
> UTC 2012 x86_64 GNU/Linux<br>
> adj@sacredchao:~$ echo $((2 * 5))<br>
> 10<br>
> adj@sacredchao:~$ echo $[2 * 5]<br>
> 10<br>
> adj@sacredchao:~$<br>
><br>
> Googling for "bashism" will turn up many similar instances where Chet<br>
> and crew added non-standard stuff to bash, and the efforts needed to<br>
> weed them out when they're used in non-bash environments.<br>
><br>
> To sum up: I disagree with the recommendation in the Bash Guide for<br>
> Beginners. Use the POSIX syntax for arithmetic. But if you can<br>
> guarantee your scripts will run on nothing but bash, do whatever<br>
> tickles your fancy.<br>
><br>
> - Aaron<br>
> _______________________________________________<br>
> clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
> For information, account preferences, or to unsubscribe see:<br>
> <a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br>
><br>
_______________________________________________<br>
clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br>
</div></div></blockquote></div><br>
</div>