Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groups of compound conditions in Bash test [duplicate]

I want to have some groups of conditions in a Bash if statement. Specifically, I'm looking for something like the following:

if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then... 

How may I group the conditions together in the way I describe for use with one if statement in Bash?

like image 501
d3pd Avatar asked Feb 19 '13 18:02

d3pd


People also ask

How do you write multiple conditions in an if statement in Unix?

To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.

What does || in bash do?

Logical OR Operator ( || ) in Bash It is usually used with boolean values and returns a boolean value. It returns true if at least one of the operands is true. Returns false if all values are false.

What are the 2 conditional statements that can be used in a shell script select two?

Two types of conditional statements can be used in bash. These are, 'If' and 'case' statements.


1 Answers

Use the && (and) and || (or) operators:

if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then 

They can also be used within a single [[ ]]:

if [[ expression && expression || expression ]] ; then 

And, finally, you can group them to ensure order of evaluation:

if [[ expression && ( expression || expression ) ]] ; then 
like image 76
William Avatar answered Oct 04 '22 18:10

William