title: Bash (via Jenkins) regex if/else
body: <p>I'm trying to extract the Jira story number from a branch name in Jenkins (1.617 on Ubuntu 12.04), so that my Maven deploy (of a web application) can use that Jira number to tag the application name. If there is no Jira story number, I would like the branch to get tagged with "bugfix".</p>
  
  <p>E.g., I check in branch <code>ABC-123_Some_Feature</code>. Jenkins picks up that there has been a checkin and builds the project. It then extracts the "ABC-123" part and deploys the application to <code>http://my-dev-server/ABC-123</code>.</p>
  
  <p>I have all of this working fine, except the regex. And specifically, the regex when running via Jenkins.</p>
  
  <p>To vastly simplify the problem, on the Ubuntu 12 machine on which our Jenkins instance is hosted, via command line, I can do this:</p>
  
  <pre><code>xxx@yyy:/$ [[ abc_ABC-123_def =~ (ABC-[[:digit:]]*) ]] &amp;&amp; echo "match" || echo "No match"
  match
  xxx@yyy:/$ [[ abc_XXX-ZZZ_def =~ (ABC-[[:digit:]]*) ]] &amp;&amp; echo "match" || echo "No match"
  No match
  </code></pre>
  
  <p>However, when I use this same script in an "Execute shell" build command in an otherwise empty Jenkins job, the "Console output" is</p>
  
  <pre><code>[EnvInject] - Loading node environment variables.
  Building in workspace /var/lib/jenkins/jobs/TEST PROJECT/workspace
  [workspace] $ /bin/sh -xe /tmp/hudson4251159992600854370.sh
  /tmp/hudson4251159992600854370.sh: 2: /tmp/hudson4251159992600854370.sh: Syntax error: "(" unexpected
  Build step 'Execute shell' marked build as failure
  Finished: FAILURE
  </code></pre>
  
  <p>I assume the the unexpected opening bracket is the start of the regex string. However, I need it to match the group. </p>
  
  <p>I've also tried wrapping the regex in double inverted commas (eg <code>"(ABC-[[:digit:]]*)"</code>, with the irritating effect that the script does not fail, but it does produce an incorrect "No match" result.</p>
  
  <p>FWIW what I want to do is this, if I can work out what is wrong with the regex script - assign the Jira number to a variable named <code>branch</code> that is used later in a build script:</p>
  
  <pre><code>[[ echo "${GIT_BRANCH#*/feature/}" =~ (ABC-[[:digit:]]*) ]] &amp;&amp; branch=echo "${BASH_REMATCH[1]}" || branch="bugfix"
  </code></pre>
  
  <p>Aside: I have "sort of" got this working via a different, similar script, however the if/else eludes me. I can get the variable <code>branch</code> to be set to <code>ABC-123</code> with this <em>if</em> it occurs in the branch name, but if not, the grep fails which in turn fails the build.</p>
  
  <pre><code>branch=$(echo "${GIT_BRANCH#*/feature/}" | grep -o  'ABC-[0-9]\+')
  </code></pre>
  
  <p>Finally, this question is similar to <a href="https://stackoverflow.com/questions/34009549/bash-check-for-a-string-in-file-path">this one</a>, but the Jenkins specific weirdness of that is unanswered.</p>