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">&lt;<a href="mailto:DLWillson@thegeek.nu" target="_blank">DLWillson@thegeek.nu</a>&gt;</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&#39;ll return to using &#39;$(( EXPRESSION ))&#39;, even though I can depend on bash for all my work.<br>
<br>
I guess that &#39;$[ EXPRESSION ]&#39; has some capabilities over the old style, but I never do anything terribly complex.<br>
<br>
In my specific application, getting some random numbers, &#39;$(( EXPRESSION ))&#39; 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 &quot;$(( RANDOM % 10 + 1 )) &quot; ; 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>
&gt; David L. Willson writes:<br>
&gt; &gt; Why are the square brackets preferred (see below)? Why are there<br>
&gt; &gt; two<br>
&gt; &gt; forms?<br>
&gt;<br>
&gt; I probably don&#39;t have this completely correct, but...  I&#39;d say the<br>
&gt; recommendation is incorrect.<br>
&gt;<br>
&gt; It is my understanding that the $(( ... )) syntax is a POSIX shell<br>
&gt; feature, while the $[ ... ] is something that bash added in the<br>
&gt; early-mid 1990s.  Given that, I try to use the $(( ... )) syntax<br>
&gt; exclusively.  The $[ .. ] syntax generally doesn&#39;t work on non-bash<br>
&gt; Bourne shells.  This may or may not be a concern for you.<br>
&gt;<br>
&gt; Please stand by while I fire up the HP-UX box...<br>
&gt;<br>
&gt; Here&#39;s what happens with $[ .. ] on a non-bash Bourne style shell:<br>
&gt;<br>
&gt;     $ SHELL=/usr/bin/sh; exec $SHELL -o vi<br>
&gt;     $ uname -a<br>
&gt;     HP-UX billandd B.11.23 U 9000/800 1822910606 unlimited-user<br>
&gt;     license<br>
&gt;     $ echo $((2 * 5))<br>
&gt;     10<br>
&gt;     $ echo $[2 * 5]<br>
&gt;     $[2 fileset-cleanup 5]<br>
&gt;     $ exit<br>
&gt;<br>
&gt; Compare with the results on a machine using bash:<br>
&gt;<br>
&gt;     adj@sacredchao:~$ echo $SHELL<br>
&gt;     /bin/bash<br>
&gt;     adj@sacredchao:~$ uname -a<br>
&gt;     Linux sacredchao 3.2.0-0.bpo.1-amd64 #1 SMP Sat Feb 11 08:41:32<br>
&gt;     UTC 2012 x86_64 GNU/Linux<br>
&gt;     adj@sacredchao:~$ echo $((2 * 5))<br>
&gt;     10<br>
&gt;     adj@sacredchao:~$ echo $[2 * 5]<br>
&gt;     10<br>
&gt;     adj@sacredchao:~$<br>
&gt;<br>
&gt; Googling for &quot;bashism&quot; will turn up many similar instances where Chet<br>
&gt; and crew added non-standard stuff to bash, and the efforts needed to<br>
&gt; weed them out when they&#39;re used in non-bash environments.<br>
&gt;<br>
&gt; To sum up: I disagree with the recommendation in the Bash Guide for<br>
&gt; Beginners.  Use the POSIX syntax for arithmetic.  But if you can<br>
&gt; guarantee your scripts will run on nothing but bash, do whatever<br>
&gt; tickles your fancy.<br>
&gt;<br>
&gt; - Aaron<br>
&gt; _______________________________________________<br>
&gt; clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
&gt; For information, account preferences, or to unsubscribe see:<br>
&gt; <a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br>
&gt;<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>