fatnjazzy
fatnjazzy

Reputation: 6152

bash cant compare strings with or statement

I have the following code:

ENVIRONMENT=production
if [ "$ENVIRONMENT" != "production" -o  "$ENVIRONMENT" != "staging" ]; then
   echo "$ENVIRONMENT is not supported, you may use production or staging"
   exit
fi

this is the output:
$ production is not supported, you may use production or staging

What am I doing wrong?

Thanks

Upvotes: 0

Views: 38

Answers (2)

Ivan
Ivan

Reputation: 7297

Use case instead it's more convenient

ENVIRONMENT=production

case "$ENVIRONMENT" in
    production|staging) : ;; # : means do nothing
    *                 ) echo "$ENVIRONMENT is not supported, you may use production or staging";;
esac

Upvotes: 1

choroba
choroba

Reputation: 241998

You need -a instead of -o. If $ENVIRONMENT is production, it still isn't staging.

Upvotes: 2

Related Questions