I'm trying to write a bash script that increments the version number which is given in
{major}.{minor}.{revision}
For example.
1.2.13
Is there a good way to easily extract those 3 numbers using something like sed or awk such that I could increment the {revision} number and output the full version number string.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1) $9 is the ninth argument.
To find my bash version, run any one of the following command: Get the version of bash I am running, type: echo "${BASH_VERSION}" Check my bash version on Linux by running: bash --version. To display bash shell version press Ctrl + x Ctrl + v.
$ v=1.2.13 $ echo "${v%.*}.$((${v##*.}+1))" 1.2.14
$ v=11.1.2.3.0 $ echo "${v%.*}.$((${v##*.}+1))" 11.1.2.3.1
Here is how it works:
The string is split in two parts.
${v%.*}
${v##*.}
The first part is printed as is, followed by a plain dot and the last part incremented using shell arithmetic expansion: $((x+1))
Pure Bash using an array:
version='1.2.33' a=( ${version//./ } ) # replace points, split into array ((a[2]++)) # increment revision (or other part) version="${a[0]}.${a[1]}.${a[2]}" # compose new version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With