0

I've got the following NODE_ENV set to testing. I can't figure out why, but for some reason this IF statement is always returning true (even if I set the NODE_ENV = testing).

if [[ $NODE_ENV != "testing" || $NODE_ENV != "development" ]]; then
  echo "❌  Not a development OR testing environment: NODE_ENV is not set to development OR testing"
  exit 1
fi

How could this be happening?

I also tried formatting the if statment like if [] || []; which didn't work either.

James111
  • 15,378
  • 15
  • 78
  • 121
  • Yes because it matches 2nd condition `$NODE_ENV != "development"` and returns true – Inian Jan 07 '18 at 04:57
  • 2
    Also you could just simplify it to `if [[ ! $NODE_ENV =~ ^(testing|development)$ ]] ; then` using the regex operator that `bash` supports with the `~`. – Inian Jan 07 '18 at 05:04

1 Answers1

1

The logic you want uses AND here, rather than OR:

if [[ $NODE_ENV != "testing" && $NODE_ENV != "development" ]]; then
    echo "❌  Not a development OR testing environment: NODE_ENV is not set to development OR testing"
    exit 1
fi

The opposite of:

$NODE_ENV == "testing" || $NODE_ENV == "development"

is:

$NODE_ENV != "testing" && $NODE_ENV != "development"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360