Extracting substring in bash by matching

Revision history
Tags: bash


In a Bitbucket Pipelines configuration I am building a release pipeline that matches a release branch by name release/*. In this build step I am building a docker image, then in turn tagging it. I want to tag it using the version in the branch name, just without the release/ part.

The branch name is available in $BITBUCKET_BRANCH, and I can use bash to extract a substring.

Extract substring

Remove matching prefix pattern ${parameter##word}

$ branch=release/branch/2.2.1; version=${branch##*/}; echo $version;

Using ##(as above) will return the longest match. Using a single # (as below) will return the shortest match.

$ branch=release/branch/2.2.1; version=${branch#*/}; echo $version;

This means I can use the following to get the image tag I want

$ docker build -t my-image:${BITBUCKET_BRANCH#release/}

To be shorter, less explicit, alas less readable for the untrained basher

$ docker build -t my-image:${BITBUCKET_BRANCH#*/}

Readability matters the most!


If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.