0

I am running this piece of code so that I can match the branch name with a pattern and create port based on which:

nano test.sh


#!/usr/bin/env bash

branch="3.2.1"

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

if [[ $branch =~ "master" ]]; then
  port=3000
  echo $port
elif [[ $branch =~ $re ]]; then
  port="300${BASH_REMATCH[0]}"
  echo $port
else
  echo "no match found!"
fi

and bash test.sh

I am expecting 3003 but it is returning 3.2.1

Amin Ba
  • 1,603
  • 1
  • 13
  • 38

1 Answers1

0

should be 300${BASH_REMATCH[1]

#!/usr/bin/env bash

branch="3.2.1"

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

if [[ $branch =~ "master" ]]; then
  port=3000
  echo $port
elif [[ $branch =~ $re ]]; then
  port="300${BASH_REMATCH[1]}"
  echo $port
else
  echo "no match found!"
fi
Amin Ba
  • 1,603
  • 1
  • 13
  • 38