Section 6.8. Arithmetic


[Page 222 (continued)]

6.8. Arithmetic

To perform an arithmetic operation in Bash, you simply put the operation inside a double set of parentheses (Figure 6-24).

Figure 6-24. Syntax of an arithmetic operation.

(( operation ))


Common numeric operations include those listed in Figure 6-25.


[Page 223]

Figure 6-25. Arithmetic operators.

+ -

Addition, subtraction.

++ --

Increment, decrement.

* / %

Multiplication, division, remainder.

**

Exponentiation.


Integer arithmetic is faster than floating-point arithmetic. If you know your variable will always be an integer (like a counter or array index), you can use the declare built-in to declare it to be an integer (Figure 6-26).

Figure 6-26. Using the declare built-in to define a variable as an integer.

Shell command: declare -i name

This form of declare defines the variable name as an integer value


After we look at some simple conditional expressions, we'll combine them into a simple math script.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net