0

So I am trying to save the output of a command into a variable and do a string comparison with that. Here's the code snippet causing the issues:

BRANCH=$(git rev-parse --abbrev-ref HEAD)

if ["$BRANCH" = 'master'] || ["$BRANCH" = 'main']; then
    echo "Create a new branch first!"
    exit 1
fi

When I run this I get the following error:

[master: command not found
[master: command not found

It seems like it is taking "$BRANCH" and trying to evaluate it rather than doing the comparison.

I've found about 10 different solutions on here on how to do the string comparison but none of them work and I get basically the same error on all of them. I've tried using == instead, using double quotes instead, using [[ and ]] instead, I've tried using "${BRANCH}" instead, and various combinations.

What do I need to do to treat $BRANCH as a string and not a command?

ATSOTECK
  • 79
  • 1
  • 5
  • 1
    You need spaces around the `[` and `]`. [shellcheck.net](https://www.shellcheck.net) is good at spotting mistakes like this. Also, I recommend using lower- or mixed-case variable names, to avoid conflicts with the many all-caps names with special meanings. – Gordon Davisson Feb 11 '22 at 19:06
  • 1
    That fixed it, thanks! – ATSOTECK Feb 11 '22 at 20:46

0 Answers0