Increment version text stored in a bash script file

bashregexsedversion

I have a bash file and I want to store a version text in it (preferably "echo version 1.0.1" so that it automatically prints out its own version when executed)

I am trying to make a command group that will update the version.

So far I've got

grep "echo version" ~/.bashrc | cut -c 14- | sed -e 's/\./\n/g'

which extracts "echo version 1.0.1", cuts out the "echo version " and splits the version between the dots

1
0
1

and I'm hitting my limit of regex, bash, and google-foo to extract the last number (patch version) which I could increment and write back to file.

Mainly, how do I get that last number?

Also if there's anything much better I should be doing, please do suggest.

Best Answer

awk -F'[ .]' '/^echo version/ {print $1,$2,$3"."$4"."$5+1}' ~/.bashrc

Output:

echo version 1.0.2

-F'[ .]': field separator(s) blank and dot

/^echo version/: search for line starting (^) with echo version

print $1,$2: print strings separated by a blank

$5+1: increment string/value by 1

Related Question