Q:

Replace and increment a line in bash

How can I read, increment and replace a value in a file?
foo="val"
ver="1.2.0001"
...

Now I would like to increment the "0001" to 0002".

A:

Assuming that the patch level is always going to be a string of four digits:
$ ver=1.2.0001
$ printf '%s\n' "$ver" | awk -F '.' '{ printf("%s.%s.%04d\n", $1, $2, $3 + 1) }'
1.2.0002

This uses awk and treats the version as three fields delimited by dots.  It prints the first two fields as they are, but adds 1 to the third field and formats the result using %04d (a zero-filled, four digit, decimal number).
This would generate 1.2.10000 if $ver was 1.2.9999.
To store the value back into ver, use ver=$( printf ... | awk ... ).

