[clue] arithmetic in bash

David L. Willson DLWillson at TheGeek.NU
Thu Apr 12 10:21:26 MDT 2012


Pardon the HTML. In this message, it adds useful features. 

I ran into the square bracket form of arithmetic expression yesterday, in a small quest for a random number, and now I have questions... 

Why are the square brackets preferred (see below)? Why are there two forms? 

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html 

<< quote_from_beginners_guide 
3.4.6. Arithmetic expansion 

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is: 
$(( EXPRESSION )) 
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. 
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: 

Table 3-4. Arithmetic operators Operator Meaning VAR++ and VAR-- 	variable post-increment and post-decrement 
++VAR and --VAR 	variable pre-increment and pre-decrement 
- and + 	unary minus and plus 
! and ~ 	logical and bitwise negation 
** 	exponentiation 
*, / and % 	multiplication, division, remainder 
+ and - 	addition, subtraction 
<< and >> 	left and right bitwise shifts 
<=, >=, < and > 	comparison operators 
== and != 	equality and inequality 
& 	bitwise AND 
^ 	bitwise exclusive OR 
| 	bitwise OR 
&& 	logical AND 
|| 	logical OR 
expr ? expr : expr 	conditional evaluation 
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^= and |= 	assignments 
, 	separator between expressions 
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. 
Constants with a leading 0 (zero) are interpreted as octal numbers. A leading "0x" or "0X" denotes hexadecimal. Otherwise, numbers take the form "[BASE'#']N" , where "BASE" is a decimal number between 2 and 64 representing the arithmetic base, and N is a number in that base. If "BASE'#'" is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, "@" , and "_" , in that order. If "BASE" is less than or equal to 36, lowercase and uppercase letters may be used interchangably to represent numbers between 10 and 35. 
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above. 
Wherever possible, Bash users should try to use the syntax with square brackets: 
$[ EXPRESSION ] 
However, this will only calculate the result of EXPRESSION , and do no tests: franky ~> echo $[365*24] 8760 
See Section 7.1.2.2 , among others, for practical examples in scripts. 
quote_from_beginners_guide 

-- 
David L. Willson 
Trainer, Engineer, Enthusiast 
RHCE Network+ A+ Linux+ LPIC-1 Ubuntu 
Mobile 720-333-LANS(5267) 

This is a good time for a r3volution. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cluedenver.org/pipermail/clue/attachments/20120412/cb786db7/attachment-0001.html 


More information about the clue mailing list