Friday, July 22, 2022

[SOLVED] How can I bump a version number using bash

Issue

I would like to know how to bump the last digit in a version number using bash.

e.g.

VERSION=1.9.0.9
NEXT_VERSION=1.9.0.10

EDIT: The version number will only contain natural numbers.

Can the solution be generic to handle any number of parts in a version number.

e.g.

1.2
1.2.3
1.2.3.4
1.2.3.4.5

Solution

TL;DR:

VERSION=1.9.0.9
echo $VERSION | awk -F. '/[0-9]+\./{$NF++;print}' OFS=.
# will print 1.9.0.10

For a detailed explanation, read on.


Let's start with the basic answer by froogz3301:

VERSIONS="
1.2.3.4.4
1.2.3.4.5.6.7.7
1.9.9
1.9.0.9
"

for VERSION in $VERSIONS; do 
    echo $VERSION | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g'
done

How can we improve on this? Here are a bunch of ideas extracted from the copious set of comments.

The trailing '1' in the program is crucial to its operation, but it is not the most explicit way of doing things. The odd '1' at the end is a boolean value that is true, and therefore matches every line and triggers the default action (since there is no action inside braces after it) which is to print $0, the line read, as amended by the previous command.

Hence, why not this awk command, which obviates the sed command?

awk -F. '{$NF+=1; OFS="."; print $0}'

Of course, we could refine things further — in several stages. You could use the bash '<<<' string redirection operator to avoid the pipe:

awk -F. '...' <<< $VERSION

The next observation would be that given a series of lines, a single execution of awk could handle them all:

echo "$VERSIONS" | awk -F. '/[0-9]+\./{$NF+=1;OFS=".";print}'

without the for loop. The double quotes around "$VERSION" preserve the newlines in the string. The pipe is still unnecessary, leading to:

awk -F. '/[0-9]+\./{$NF+=1;OFS=".";print}' <<< "$VERSIONS"

The regex ignores the blank lines in $VERSION by only processing lines that contain a digit followed by a dot. Of course, setting OFS in each line is a tad clumsy, and '+=1' can be abbreviated '++', so you could use:

awk -F. '/[0-9]+\./{$NF++;print}' OFS=. <<< "$VERSIONS"

(or you could include 'BEGIN{OFS="."}' in the program, but that is rather verbose.

The '<<<' notation is only supported by Bash and not by Korn, Bourne or other POSIX shells (except as a non-standard extension parallelling the Bash notation). The AWK program is going to be supported by any version of awk you are likely to be able to lay hands on (but the variable assignment on the command line was not supported by old UNIX 7th Edition AWK).



Answered By - Jonathan Leffler
Answer Checked By - Senaida (WPSolving Volunteer)