-1

I have a shell script for updating a project's version automatically on commit that's working perfectly on zsh (on macOS Catalina) and not working on bash (on Ubuntu 20.04).

When running the script, I get the following error:

Syntax error: "(" unexpected (expecting "then")

On the following line:

if [[ $commit_title =~ (into )([A-Za-z0-9-]+) ]]
then
  merge_branch=${BASH_REMATCH[2]}
else
  merge_branch=$main_branch
fi

And if I remove the block of code that is using bash rematch I get the following error:

1: eval: Syntax error: "(" unexpected

I use eval in multiple places, for example:

if eval '[[ $current_branch =~ '"^($git_flow_from)$"' ]]' && eval '[[ $merge_branch =~ '"^($git_flow_to)$"' ]]'
then
  echo 'Skipping version update for this merge...'
  exit 0
fi

You can see the whole script here for context (I did not include it in the question because it is a bit long): https://github.com/celiavelmar/conventional-pre-commits/blob/master/scripts/update_version.sh

What am I doing wrong? Thanks in advance!

  • 1
    Are you sure you're running the script in bash? Note that `/bin/sh` on Ubuntu is `dash`, not `bash`. – choroba Oct 03 '20 at 19:02
  • Try checking your script at [shellcheck.net](https://www.shellcheck.net/) ... shows problems with fix suggestions, and even suggested improvements. – Paul T. Oct 03 '20 at 19:09
  • @choroba I have `#!/bin/bash` as the first line on my script. – Celia Velasco Oct 03 '20 at 19:45
  • @CeliaVelasco: And how do you run the script? – choroba Oct 03 '20 at 19:50
  • @choroba Ok... I was running the script via `sh ./scripts/...`. With `bash ./scripts/...` it's working perfectly fine. Thank you so much! – Celia Velasco Oct 03 '20 at 20:02
  • @CeliaVelasco It's generally best not to use either `sh` or `bash` (or whatever) to run scripts, just set the shebang right, add execute permission to the script, and run it with just `./scripts/...` -- this lets the shebang control execution, so you don't have to know which dialect the script is written in. BTW, are those `eval`s really necessary? They look like an opportunity for something to go weirdly wrong. – Gordon Davisson Oct 03 '20 at 20:05
  • @GordonDavisson I used `eval` because some regular expressions include variables that can be configured by the user and after many attempts that was the only way I got it to work. – Celia Velasco Oct 03 '20 at 20:10

1 Answers1

1

I noticed thanks to @choroba's comment on my question that I was running the script with sh instead of bash. This caused the script to run on dash on Ubuntu and fail since it uses bash features not available in other shells.