This is a multipart blog article series where I am going to explain the concepts of shell scripting and how to write a shell script in Linux, UNIX or Mac based systems. You can also follow this tutorial blog using windows but for that you have to install a bash from.
In this article we will see that how can we use ‘AND’ operator with the help of if condition. We will also see that different ways to use ‘AND’ operator.
We are going to use AND operator with if condition to solve a problem.
The problem is, we have to print valid age if the age is between 18 to 30 years, and if not then print invalid age.
- If the age is greater than 18 then AND smaller than 30 then its successful/ true
- Otherwise it’s false/ unsuccessful.
- The syntax of if condition for this problem will be
if [ “$<variable_name>” –gt 18 ] && [ “$<variable_name>” –lt 30 ]
- There are two ways to use AND operator, we are using
&&in this example.
- If AND operator is used then both conditions should be true, in order to get true otherwise it will return false.
- Another way to use AND operator is
if [ “$<variable_name>” –gt 18 –a “$<variable_name>” –lt 30 ]
- In this way we use
-aflag and it will work same as
&&but the syntax to write the; if condition gets change little bit.
- If you want to use
&&operator then there is one more way to do this, and that is by using double square brackets like this
if [[ “$<variable_name>” –gt 18 && “$<variable_name>” –lt 30 ]]
So this was all about using AND operator with if condition. Hope you liked it and learned something new from it.
If you have any doubt, question, quires related to this topic or just want to share something with me, then please feel free to contact me.