Bash if Statement with Multiple Conditions

Written by: Linuxopsys   |   Last updated: April 17, 2023

While often writing Bash scripts, we want to have more than one possible flow of control based on the conditions. The if condition lets us conditionally process the statements by implicitly converting the test expressions to bool. Using the if statements, coupled with multiple conditions we can make logical comparisons between a value and what we expect. We can optionally specify an else clause as well with more than one condition. 

In this tutorial, we will learn how to concatenate multiple conditions with the if statement.

Multiple conditions

At times the test expressions, with the if statement, may not be that simple and straightforward. We might need to join multiple expressions to develop the conditions for the code flow. For such scenarios, involving more than 1 condition, we can use different approaches.

Using bash logical operators

A logical operator connects two or more expressions in such a way that the value of the compound expression depends only on that of the original expressions and on the operator. These logical operators in Bash are AND (&&) and OR (||).

Syntax

[ EXPR1 ] && [ EXPR2 ]    True if both EXPR1 and EXPR2 are true.
[ EXPR1 ] || [ EXPR2 ]    True if either EXPR1 or EXPR2 is true.

Example

num=150
if [ $num -gt 100 ] && [ $num -lt 200 ]; then echo true; else echo false; fi
using AND (&&) and OR (||)

Bash splits the expression into simpler ones and then evaluates each of them separately. Once done, it then looks at the logical operators to reassemble those expressions to figure out the result of the original expression. In this example the integer value is present with the variable num. The if condition can be considered as 2 separate if statements. The first one checks if the variable is greater than 100, which is true. The second checks if the number is less than 200, which is also true. As both are true and we have a logical AND, the result becomes true as well. This triggers the statements present inside the "then" block.

Using single test alias

Bash lets us combine multiple conditions in one test alias using the logical operator. We need to use -a (for and) and -o (for or) operations. The evaluation happens as follows:

Syntax

[ EXPR1 -a EXPR2 ]	True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]	True if either EXPR1 or EXPR2 is true.

Example

num=150
if [ $num -gt 100 -a $num -lt 200 ]; then echo true; else echo false; fi
if [ $num -gt 100 -o $num -lt 50 ]; then echo true; else echo false; fi

Bash uses the same logic as we saw in the previous example where it splits the expression into simpler ones and then evaluates each of them separately. It then checks the operators and reassembles these expressions to compute the result of the original expression. This example can be considered as 2 separate if statements joined together with the -a flag. As both are true, the result is true and the "then" block executes.

Similarly in the next case, the first part of the expression is true and the second part is false. Combining them -o flag will give the result as true, if the result of any expression is true.

Using double square brackets

Another way of having multiple conditions with the if statement is to use the double bracket notation. 

Syntax

[[ EXPR1 && EXPR2 ]]    True if both EXPR1 and EXPR2 are true.
[[ EXPR1 || EXPR2 ]]    True if either EXPR1 or EXPR2 is true.

Example

num=150
if [[ $num -gt 100 && $num -lt 200 ]]; then echo true; else echo false; fi
bash using double bracket notation. 

This returns a status of 0 or 1 depending on the evaluation of the conditional expression. The expressions are made up of the same primaries used by the test alias we saw above. However, we need to use the && (for AND) and || (for OR) for combining multiple expressions. The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to determine the original expression's value. This example has 2 separate expressions, both true, joined by the logical and. Hence true is displayed on the terminal.

Using individual double square brackets

We can also use individual double square brackets to evaluate multiple conditions inside if.

Syntax

[ EXPR1 ] && [ EXPR2 ]    True if both EXPR1 and EXPR2 are true.
[ EXPR1 ] || [ EXPR2 ]    True if either EXPR1 or EXPR2 is true.

Example

num=150
if [[ $num -gt 100 ]] && [[ $num -lt 200 ]]; then echo true; else echo false; fi
bash individual double square brackets

We can consider double square brackets as an alternative to single brackets. However, unlike single brackets, it’s possible to have the comparison operators (>, < etc) with the double brackets. We should use the && operator for the logical AND operation and the || operator for the logical OR operations. As is the case everywhere, Bash splits the expression into simpler ones and then evaluates each of them separately. It then checks the logical operators to reassemble those expressions to compute the result of the original expression. 

Conclusion

  • Bash can have multiple conditions with the if statement.
  • Individual expressions can be combined by using logical operators. 
  • We have different ways of developing the if statement with multiple expressions.
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment