In bash, we have logical or and Logical and operator. These compound bash operators can work with two or more expression. So let’s start and see how they work in bash and how they can help to solve problems while writing scripts in bash.

Bash Compound operators using && and ||

These compound operator can operate on two or more expression to get their result on basis on their functions. In or operator, if any of expression is true, then it return true value, in reveres and operator will return true only if all expressions are true.

or operator ||
Let’s see few examples for or operator in one liner and script format.

#[ 4 -le 5 ] || [ 5 -le 7 ]; echo $?
0
#[ 4 -le 5 ] || [ 9 -le 7 ]; echo $?
0
#[ 6 -le 5 ] || [ 9 -le 7 ]; echo $?
1

With above example you may observe that first two expressions have at least one true value, hence returns true value. But in last both expressions are false, hence returns false value
we can use more than two expression as well, below some of examples for same.

#[ 4 -le 5 ] || [ 5 -le 7 ] || [3 -le 7]; echo $?
0
#[ 4 -le 5 ] || [ 8 -le 7 ] || [9 -le 7]; echo $?
0
#[ 34 -le 5 ] || [ 8 -le 7 ] || [ 9 -le 7 ]; echo $?
1

Same way they work in if condition and can play important rule for conditional statements.

and operator &&
Let’s see few examples for and operator in one liner and script format.

#[ 4 -le 5 ] && [ 6 -le 7 ]; echo $?
0
#[ 4 -le 5 ] && [ 45 -le 7 ]; echo $?
1

With above example, you may know that in first statement both expressions are true,hence it returns true. But in last statement one expression is true and another is false, hence it returns false.

In same way we can use two or more expression with and operator use.