I'm using cut function for getting all sub strings. For example: I have a string named "v1.2.3". I want assign 1 to major. 2 to minor and 3 to bug (remove first character is always v)
For example below:
major=$(echo $tag | cut -d'.' -f1)
minor=$(echo $tag | cut -d'.' -f2)
bug=$(echo $tag | cut -d'.' -f3)
echo "$major $minor $bug"
This script expands on 3 lines. My question is: how can I return all f1
f2
and f3
in one call and assign back to major
minor
and bug
at same time.
I also try to use regular expression. for example: v1.2.3
will be split to 1,2 and 3 respectively but it seems not work.
re="^v(.*).(.*).(.*)$"
[[ $tag =~ $re ]] && major="${BASH_REMATCH[1]}" && minor="${BASH_REMATCH[2]}" && patch="${BASH_REMATCH[3]}"
echo $major
echo $minor
echo $patch
thanks.