<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: Times New Roman; font-size: 12pt; color: #000000'>Pardon the HTML. In this message, it adds useful features.<br><br>I ran into the square bracket form of arithmetic expression yesterday, in a small quest for a random number, and now I have questions...<br><br>Why are the square brackets preferred (see below)? Why are there two forms?<br><br>http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html<br><br>&lt;&lt; quote_from_beginners_guide<br><h2 class="sect2">3.4.6. Arithmetic expansion</h2><p>Arithmetic 
expansion allows the evaluation of an arithmetic expression and the 
substitution of the result.  The format for arithmetic expansion is:</p><p><b class="command">$(( EXPRESSION ))</b> </p><p>The
 expression is treated as if it were within double quotes, but a double 
quote inside the parentheses is not treated specially.  All tokens in 
the expression undergo parameter expansion, command substitution, and 
quote removal.  Arithmetic substitutions may be nested.</p><p>Evaluation
 of arithmetic expressions is done in fixed-width integers with no check
 for overflow - although division by zero is trapped and recognized as 
an error.  The operators are roughly the same as in the C programming 
language.  In order of decreasing precedence, the list looks like this:</p><div class="table"><a name="table_03_04"></a><p><b>Table 3-4. Arithmetic operators</b></p><table class="CALSTABLE" border="1"><thead><tr><th valign="MIDDLE" align="LEFT">Operator</th><th valign="MIDDLE" align="LEFT">Meaning</th></tr></thead><tbody><tr><td valign="MIDDLE" align="LEFT">VAR++ and VAR--</td><td valign="MIDDLE" align="LEFT">variable post-increment and post-decrement</td></tr><tr><td valign="MIDDLE" align="LEFT">++VAR and --VAR</td><td valign="MIDDLE" align="LEFT">variable pre-increment and pre-decrement</td></tr><tr><td valign="MIDDLE" align="LEFT">- and +</td><td valign="MIDDLE" align="LEFT">unary minus and plus</td></tr><tr><td valign="MIDDLE" align="LEFT">! and ~</td><td valign="MIDDLE" align="LEFT">logical and bitwise negation</td></tr><tr><td valign="MIDDLE" align="LEFT">**</td><td valign="MIDDLE" align="LEFT">exponentiation</td></tr><tr><td valign="MIDDLE" align="LEFT">*, / and %</td><td valign="MIDDLE" align="LEFT">multiplication, division, remainder</td></tr><tr><td valign="MIDDLE" align="LEFT">+ and -</td><td valign="MIDDLE" align="LEFT">addition, subtraction</td></tr><tr><td valign="MIDDLE" align="LEFT">&lt;&lt; and &gt;&gt;</td><td valign="MIDDLE" align="LEFT">left and right bitwise shifts</td></tr><tr><td valign="MIDDLE" align="LEFT">&lt;=, &gt;=, &lt; and &gt;</td><td valign="MIDDLE" align="LEFT">comparison operators</td></tr><tr><td valign="MIDDLE" align="LEFT">== and !=</td><td valign="MIDDLE" align="LEFT">equality and inequality</td></tr><tr><td valign="MIDDLE" align="LEFT">&amp;</td><td valign="MIDDLE" align="LEFT">bitwise AND</td></tr><tr><td valign="MIDDLE" align="LEFT">^</td><td valign="MIDDLE" align="LEFT">bitwise exclusive OR</td></tr><tr><td valign="MIDDLE" align="LEFT">|</td><td valign="MIDDLE" align="LEFT">bitwise OR</td></tr><tr><td valign="MIDDLE" align="LEFT">&amp;&amp;</td><td valign="MIDDLE" align="LEFT">logical AND</td></tr><tr><td valign="MIDDLE" align="LEFT">||</td><td valign="MIDDLE" align="LEFT">logical OR</td></tr><tr><td valign="MIDDLE" align="LEFT">expr ? expr : expr</td><td valign="MIDDLE" align="LEFT">conditional evaluation</td></tr><tr><td valign="MIDDLE" align="LEFT">=, *=, /=, %=, +=, -=, &lt;&lt;=, &gt;&gt;=, &amp;=, ^= and |=</td><td valign="MIDDLE" align="LEFT">assignments</td></tr><tr><td valign="MIDDLE" align="LEFT">,</td><td valign="MIDDLE" align="LEFT">separator between expressions</td></tr></tbody></table></div><p>Shell
 variables are allowed as operands; parameter expansion is performed 
before the expression is evaluated.  Within an expression, shell 
variables may also be referenced by name without using the parameter 
expansion syntax.  The value of a variable is evaluated as an arithmetic
 expression when it is referenced.  A shell variable need not have its 
integer attribute turned on to be used in an expression.</p><p>Constants with a leading 0 (zero) are interpreted as octal numbers.  A
leading <span class="QUOTE">"0x"</span> or <span class="QUOTE">"0X"</span> denotes hexadecimal.  Otherwise, numbers take the form <span class="QUOTE">"[BASE'#']N"</span>, where <span class="QUOTE">"BASE"</span> is a decimal number between 2 and 64 representing the arithmetic base, and N is a number in that base.  If <span class="QUOTE">"BASE'#'"</span> is omitted, then base 10 is used.  The digits greater than 9 are represented by the lowercase letters, the uppercase letters, <span class="QUOTE">"@"</span>, and <span class="QUOTE">"_"</span>, in that order.  If <span class="QUOTE">"BASE"</span> is less than or equal to 36, lowercase and uppercase letters may be used interchangably to represent numbers between 10 and 35.</p><p>Operators
 are evaluated in order of precedence.  Sub-expressions in parentheses 
are evaluated first and may override the precedence rules above.</p><p>Wherever possible, Bash users should try to use the syntax with square brackets:</p><p><b class="command">$[ EXPRESSION ]</b> </p><p>However, this will only calculate the result of <em>EXPRESSION</em>, and do no tests:</p><table width="100%" bgcolor="#E0E0E0" border="0"><tbody><tr><td><font color="#000000"><pre class="screen"><tt class="prompt">franky ~&gt;</tt> <b class="command">echo <tt class="parameter"><i>$[365*24]</i></tt></b>
8760
</pre></font></td></tr></tbody></table><p>See <a href="http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html#sect_07_01_02_02">Section 7.1.2.2</a>, among others, for practical examples in scripts.</p><br>quote_from_beginners_guide<br><br><span><span name="x"></span>--<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.<span name="x"></span><br></span><br></div></body></html>