![]() |
![]() |
Do not confuse the
arithmetic operators
plus
minus
multiplication
division
exponentiation
modulo, or mod (returns the remainder of an integer division operation)
This operator finds use in, among other things, generating numbers within a specific range (see Example 9-24 and Example 9-27) and formatting program output (see Example 26-15 and Example A-6). It can even be used to generate prime numbers, (see Example A-16). Modulo turns up surprisingly often in various numerical recipes.
Example 8-1. Greatest common divisor
let "var += 5" results in var being incremented by 5.
let "var *= 4" results in var being multiplied by 4.
Arithmetic operators often occur in an expr or let expression.
Example 8-2. Using Arithmetic Operations
Integer variables in Bash are actually signed long (32-bit) integers, in the range of -2147483648 to 2147483647. An operation that takes a variable outside these limits will give an erroneous result.
As of version 2.05b, Bash supports 64-bit integers.
Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings. Use bc in scripts that that need floating point calculations or math library functions.
bitwise operators. The bitwise operators seldom make an appearance in shell scripts. Their chief use seems to be manipulating and testing values read from ports or sockets.
bitwise operators
bitwise left shift (multiplies by 2 for each shift position)
let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)
bitwise right shift (divides by 2 for each shift position)
bitwise and
bitwise OR
bitwise negate
bitwise NOT
bitwise XOR
logical operators
and (logical)
or (logical)
Bash tests the exit status of each statement linked with a logical operator.
Example 8-3. Compound Condition Tests Using && and ||
The
miscellaneous operators
comma operator
The
The comma operator finds use mainly in for loops. See Example 10-12.
You Will Learn Algebra Better - Guaranteed! Just take a look how incredibly simple Algebrator is: Step 1 : Enter your homework problem in an easy WYSIWYG (What you see is what you get) algebra editor: Step 2 : Let Algebrator solve it: Step 3 : Ask for an explanation for the steps you don't understand:
Algebrator can solve problems in all the following areas:
Goods and services provided by Sofmath (TX, USA). |
| ||||||