26 January 2012

Arithmetic Expansion

The shell allows arithmetic to be performed by expansion. This allows us to use the shell prompt as a calculator:

[me@linuxbox ~]$ echo $((2 + 2))
4

Arithmetic expansion uses the following form:
$((expression))

where expression is an arithmetic expression consisting of values and arithmetic operators.

Arithmetic expansion supports only integers (whole numbers, no decimals) but can perform quite a number of different operations.

Table lists a few of the supported operators.




Spaces are not significant in arithmetic expressions, and expressions may be nested. For example, multiply 5² by 3:

[me@linuxbox ~]$ echo $(($((5**2)) * 3))
75

Single parentheses may be used to group multiple subexpressions. With this technique, we can rewrite the example above and get the same result using a single expansion instead of two:

[me@linuxbox ~]$ echo $(((5**2) * 3))
75

Here is an example using the division and remainder operators. Notice the effect of integer division:

[me@linuxbox ~]$ echo Five divided by two equals $((5/2))
Five divided by two equals 2

[me@linuxbox ~]$ echo with $((5%2)) left over.
with 1 left over.


See Also:





biOos

No comments: