Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Writing an automatic release script, I have the current version in a variable $VERSION, and I would like to calculate the next version as $NEXT_VERSION.

Rule: Increment the integer at the end of the string.
4.0beta12 → 4.0beta13
4.2alpha9 → 4.2alpha10

I have tried with sed/awk/eval but can't make it work. Ideas? Any tool is fine.

share|improve this question
up vote 5 down vote accepted

Assuming you always have some reasonable well-formedness rules (always a non-digit in the version, version always ends in digits), this, however klunky, might be a start:

#!/usr/bin/gawk -f
1 {
    pfx = gensub(/^(.*[^0-9])([0-9]+)$/, "\\1", "")
    patchlevel = gensub(/^(.*[^0-9])([0-9]+)$/, "\\2", "")

    new_pl = (1*patchlevel)+1
    printf "%s%s\n", pfx, new_pl


[0 1066] ~/temp % echo "12.4alpha9" | gawk -f incvers.awk
[0 1067] ~/temp % echo "12.4alpha12" | gawk -f incvers.awk
[0 1068] ~/temp % echo "12.4alpha-2" | gawk -f incvers.awk
share|improve this answer

No need for anything but Bash:

for version in 12.4alpha9 12.4alpha12 12.4alpha-2; do
    echo -n "$version => ";
    [[ "$version" =~ (.*[^0-9])([0-9]+)$ ]] && version="${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))";
    echo "$version";

The only line really relevant to you, is the one with the regex check [[: it remembers everything leading up to the final number, reuses that and appends the next number.

share|improve this answer
Inside arithmetic evaluation no need to use ${} around the variable: $((BASH_REMATCH[2] + 1)) is enough. – manatwork Oct 24 '11 at 9:44
Thanks, I did not know that! – janmoesen Oct 24 '11 at 19:03

Try having


echo "Next version is $NEXT_VERSION"
# other release stuff here 

Do want this to be a self-rewriting script? I'd suggest something like

tempfile=$( tempfile -d . )
sed -re "s/POINT=[0-9]+/POINT=$(( ++POINT ))/" $0 > $tempfile
chmod +x $tempfile
mv $tempfile $0
share|improve this answer

Here's a portable shell snippet that adds 1 to the number at the end of $current_version.

n=${current_version##*[!0-9]}; p=${current_version%%$n}

Just because it's simple (well, fairly simple) doesn't mean it's a good idea. Like Andrew, I recommend breaking down your variable definitions into components.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.