Incrementing and decrementing numeric variables in bash

There are quite a few ways to increment and decrement numeric variables in bash. This post examines the many ways you can do this.

calculator numbers math accounting
fancycrave1 (CC0)

When preparing scripts that will run in bash, it’s often critical to be able to set up numeric variables that you can then increment or decrement as the script proceeds. The only surprising part of this is how many options you have to choose from to make the increment or decrement operation happen.

Incrementing numeric variables

First, to increment a variable, you first need to set it up. While the example below sets the variable $count to 1, there is no need to start at 1.

$ count=1

This would also work:

$ count=111

Regardless of the initial setting, you can then increment your variable using any of the following commands. Just replace $count with your variable name.

count=$((count+1))
((count=count+1))
let "count=count+1"
let "count++"
let "++count"
((count+=1))
((count++))
((++count))

After running any of the above commands, $count should be one larger than it was before the command was run.

As you likely suspect, except for the ++ command options, you can increase the value of a numeric value by more than one whenever you want. Try all of the commands shown below and you will see the end result. The final result should be 9 as the variable is incremented in each of the eight commands following the "count=1" command.

count=1
count=$((count+1))
((count=count+1))
let "count=count+1"
let "count++"
let "++count"
((count+=1))
((count++))
((++count))
echo $count

In the example script below, the result would be 15 since three of the commands add more than 1 to $count.

#!/bin/bash

count=1
count=$((count+1))
((count=count+2))        # add 2
let "count=count+3"      # add 3
let "count++"
let "++count"
((count+=4))             # add 4
((count++))
((++count))
echo $count

Decrementing numeric variables

Decrementing numbers can be done in the same way. You just need to use minus signs in place of the plus signs shown above. Here are the commands for decrementing the value of $count:

count=$((count-1))
((count=count-1))
let "count=count-1"
let "count--"
let "--count"
((count-=1))
((count--))
((--count))

If $count starts with a value of 20, it ends up being 12 when all of the commands shown above have been run.

Except for the -- command options, you can decrease the value of a numeric value by more than one when needed. Here’s an example:

$ count=11
$ ((count-=5))
$ echo $count
6

Incrementing variables using Loops

If you want to loop through a series of commands in a script, you can increment a numeric variable to determine when the loop will end.  The for loop shown below will end once $count reaches 6 and each pass through the loop will display lines in a file named "myfile", one more line each time through the loop.

#!/bin/bash

count=1
while [ $count -le 5 ]
do
  head -$count myfile
  ((count++))
  echo
done

Decrementing variables in loops

You can do the reverse of what is shown about with nearly the same code. This time, the $count variable starts at 5 and the loop exits when $count reaches 0.

#!/bin/bash

count=5
while [ $count -ge 1 ]
do
  head -$count myfile
  ((count--))
done

Wrap-up

Incrementing and decrementing numeric variables in bash is easy, but there are a lot more options than you likely expected.

Related:

Copyright © 2023 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022