If statements in Linux Shell Script

If statements are important in any programming language. I had a bit of hard time while doing if statements in linux since they are different than those in a conventional programming language. First of all, an if statement to compare whether a value in a variable val1 is equal to 5 is like:val1=5if [ $val1 -eq 5 ]thenCode here to do some stuffelseIf above condition is false, then code here gets executedfi

Note that spaces are important when defining condition. $val1 refers to the variable and -eq simply implies equal to operator to compare if the $val1 is equal to 5.

Seems pretty easy? It is. Other conditional operators are:

Conditional Operator

Linux Equivalent

Greater than

-gt

Less than

-lt

Equal to

-eq

Not equal to

-ne

Now, if I want to compare strings, for instance, if variable answer is equal to “yes” then the code will be as follows:if [ "$val" = "yes" ]thenCode here to do some stufffi

Again, spaces are important here.
In here, ‘=’ can easily be replaced by conventional conditional operators like ‘>’ or ‘<‘ or ‘!=’ etc.