Linux and Networking

Linux and Networking

4.17. Logical operators.

Logical operators will be extremely useful when creating shell scripts since they allow two expressions to be connected within the same statement (if, while, until, etc.).

 

The logical operators used for this purpose are logical AND (&&) and logical OR (||):

 

if [ $condition ] && [ $condition ] #use of logical AND

if [ $condition ] || [ $condition ] #use of logical OR

Let’s see an example of how they are used in a shell script:

 

Another option if we wanted to use OR, would be to have replaced the fourth line with:

 

if [ “$a” -eq 5 ] || [ “$b” -eq 3 ]

 

The Demeter Project