Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • Multiple conditions
  • Conclusion

Bash if Statement with Multiple Conditions

PreviousDetermining if a Bash String is EmptyNextMeaning of Shebang in Bash Scripts

Last updated 1 year ago

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=150if [ $num -gt 100 ] && [ $num -lt 200 ]; then echo true; else echo false; fi

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=150if [ $num -gt 100 -a $num -lt 200 ]; then echo true; else echo false; fiif [ $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=150if [[ $num -gt 100 && $num -lt 200 ]]; then echo true; else echo false; fi

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=150if [[ $num -gt 100 ]] && [[ $num -lt 200 ]]; then echo true; else echo false; fi

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.

using AND (&&) and OR (||)
bash using double bracket notation.
bash individual double square brackets