Bash: How to Increment a Version Number


Often you may want to increment a version number in Bash.

You can use the following syntax to do so:

new_version=$(echo ${my_version} | awk -F. -v OFS=. '{$NF += 1 ; print}')

This particular example increments the version number stored in the variable named my_version by one and saves the results to a new variable named new_version.

For example, a version number of 1.1.0.3 would become 1.1.0.4.

The following example shows how to use this syntax in practice.

Example: How to Increment a Version Number in Bash

Suppose that we have a string named my_version with the following version number stored in it:

  • 1.1.0.3

Suppose that we would like to increment the version number by 1 to obtain the following new version number:

  • 1.1.0.4

We can use the following syntax to do so:

my_version='1.1.0.3'

new_version=$(echo ${my_version} | awk -F. -v OFS=. '{$NF += 1 ; print}')

The following screenshot shows how to use this syntax in practice:

Bash increment version number

Notice that when we use the echo statement to view the new_version variable we can see that it has been incremented to 1.1.0.4.

Note that version numbers are stored in formats where the Major, Minor, Build and Revision numbers are separated by periods.

In this particular example we incremented the Revision number by one, but you can use similar syntax to increment a version number that is stored in a different format.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Sort Strings with Numbers
Bash: How to Find All Files that End with Number
Bash: How to Pad Numbers with Leading Zeros

Leave a Reply