Search code examples
bashparameter-expansion

bash parameter substitute digit one or more times


I have a version string, for example

version=1.2.23b2

I want to have only the last part with the b2, so I tried this:

${version##*[0-9]*.[0-9]*.[0-9]*} 

My thought was that * would mean no or many repetitions of the leading pattern, but in fact it means any character no or many times. So I want my pattern to express, that this is only about digits, one ore more times.


Solution

  • * in glob pattern matches any character 0 or more times.

    You can use extended glob pattern here:

    shopt -s extglob 
    echo "${version##+([0-9.])}"
    b2
    

    Or else, you can use regex here:

    version='1.2.23b2'
    re='^[0-9]+.[0-9]+.[0-9]+(.+)'
    [[ $version =~ $re ]] && echo "${BASH_REMATCH[1]}"
    
    b2
    

    Even this regex should work above:

    re='^[0-9.]+(.+)'