Americas

  • United States
sandra_henrystocker
Unix Dweeb

Incrementing and decrementing numeric variables in bash

How-To
Sep 05, 20233 mins
Linux

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

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.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author