The (()) structure may be used for evaluating arithmetic expressions or to write loops, as in the C programming language. I will discuss arithmetic expressions first. Earlier, I presented the expr command. This is good for making basic operations, but it is limited, and you need to remember to leave extra spaces between the operators and the numbers.
To make operations simpler, a new syntax has been introduced into Bash with version 2.04. However, the presence of this is not guaranteed, and if your goal is to write portable code, you should use the expr command. The syntax of this is:
variable=$(( arithmetic expression))
So all you need to do is double the parenthesis after a dollar sign. The arithmetic expressions rule closely follows the C language. Anything that would work there is possible here as well. Moreover, we can even use this to test for integer numbers (of course, we will not add the dollar sign now):
i = 2;
while (( i < 10))
do
echo $i
((--$i))
echo $i
i=$((i+2))
done
You can use any operator from the C language. In the case of the Bash shell, it will always use integer numbers, while the Korn shell will use double precision real numbers. Using the dollar sign in front of the variables inside this structure is not mandatory. Nevertheless, if you do use it, that will not be a problem. Simply put, it is optional. This is true only for your variables with names. When you want to refer to the special variables like the $1, you will have to add it.
a=$(($1+1))
b=$((a*2))
Inside the double parentheses there are not valid meta-characters of the shell. Therefore, you can use the * for multiplication, just like in the C language. There is no need to precede it with a character. Using the assigning operators like the +=, -=, *= and so forth will require you to leave out the dollar sign prior to the variable.
((b-=3))
You can use parentheses to clarify an expression. The evaluation of tests (like the >, <, <= and so forth) inside the structure is one. Nevertheless, once we get out of this, the shell will change the $? special variable to the negation of this (therefore to zero if it was one). This allows us to synchronize the true statement from the C language with the one inside the shell. This reveals why the upper while loop works.
The operator for raising a number to a power is **(two * following closely one after another). Aside from this operator, the lack or addition of spaces inside the structure will have no effect. Therefore, the following two examples are both true and will produce the same end:
a=$(( 2 ** 3 ))
a=$( 2**3 ))
We can use this even to make a C style for loop. In practice, the syntax of this strongly follows the syntax of the basic for loop in the shell, but the list will be replaced with a C style for:
for (( initial value; do until true; execute after each loop))
do
#add here the command lines
done
For instance:
for (( i=10; i > -1; --i))
do
#print a list of this
echo $i
done
This will give us an easier way to pass through a sequence of integer numbers. Then again, you sacrifice a little portability. It is up to you if you are willing to pay the price, as most of the new UNIX-based distribution will have this.