Command output to string comparison in Zsh

12 Views Asked by At

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?

0

There are 0 best solutions below