Home > OS >  Bash bad variable substitution to extract a major number from a release
Bash bad variable substitution to extract a major number from a release

Time:08-29

I have a CI_COMMIT_TAG variable with the following sample value: v12.23.34.

I would like to extract the major part number of the tag on CI_RELEASE_MAJOR, meaning 12.

Following this documentation, I did the following:

export CI_RELEASE_MAJOR=${${CI_COMMIT_TAG#v}%%.*}

This is working on zsh, but not on bash, giving the following error:

CI_RELEASE_MAJOR=${${CI_COMMIT_TAG#v}%%.*}: bad substitution

Why is it not working on bash and what is the proper way to achieve what I want?

Thanks!

CodePudding user response:

Why is it not working on bash

Because it's not possible to nest expansions like that.

what is the proper way to achieve what I want?

The proper way is:

tmp=${CI_COMMIT_TAG#v}
CI_RELEASE_MAJOR=${tmp%%.*}

CodePudding user response:

An alternative is to use regular-expression matching.

[[ $CI_COMMIT_TAG =~ v([^.])*\. ]]
export CI_RELEASE_MAJOR=${BASH_REMATCH[1]}
  • Related