Parameter manipulation

Perhaps a little more useful to us in scripting is the C-style parameter manipulation that we can include using the double parenthesis. We can often use this to increment a counter within a loop and also put a limit on the number of times the loop iterates. Consider the following command:

$ COUNT=1
$ (( COUNT++ ))
echo $COUNT

Within this example, we first set COUNT to 1 and then we increment it with the ++ operator. When it is echoed in the final line, the parameter will have a value of 2. We can see the results in the following screenshot:

We can achieve the same result in longhand by using the following syntax:

$ COUNT=1
$ (( COUNT=COUNT+1 ))
echo $COUNT

This of course allows for any increment of the COUNT parameter and not just a single unit increase. Similarly, we can count down using the -- operator, as shown in the following example:

$ COUNT=10
$ (( COUNT-- ))
echo $COUNT

We start using a value of 10, reducing the value by 1 within the double parentheses.

Note that we do not use the $ to expand the parameters within the parentheses. They are used for parameter manipulation and, as such, we do not need to expand parameters explicitly.