Suan
Suan

Reputation: 37175

How to do complex conditionals in Bash (mix of 'and' &&, 'or' || ...)

How do I accomplish something like the following in Bash?

if ("$a" == "something" || ($n == 2 && "$b" == "something_else")); then
  ...
fi

Upvotes: 25

Views: 22426

Answers (2)

Niklas B.
Niklas B.

Reputation: 95318

You almost got it:

if [[ "$a" == "something" || ($n == 2 && "$b" == "something_else") ]]; then

In fact, the parentheses can be left out because of operator precedence, so it might also be written as

if [[ "$a" == "something" || $n == 2 && "$b" == "something_else" ]]; then

Upvotes: 37

Suan
Suan

Reputation: 37175

if [[ "$a" == "something" ]] || [[ $n == 2 && "$b" == "something_else" ]]; then
  ...
fi

Upvotes: 3

Related Questions