Let’s be honest, bash programming can be really very confusing. Because it is unlike any other programming language. And when it comes to conditionals in bash programming the situation can get rough pretty quick.
But here I have tried to pretty clearly explain about the cases and conditions with examples. Hope you can follow along and this article will help you with whatever you are doing.
THE BASIC FLOW OF CONDITIONALS
Let us first briefly understand about the conditionals. If you are unfamiliar or new to programming it might help a bit. If you already are familiar with it you can skip this part.
Let us look at a pseudo code:
if <some condition> is <true> then <do something> else <do another thing> end if
If you look at the code above you see that it is rubbish block. But take a look at the flow of logic. The logic is simply if some condition is met run some code. But if the condition is not met do something else.
Now that we have understood about the basics of condition in language independent form, let us continue. And let us look at the same code but in bash without fancy expressions.
if [ expression1 ] then # do something elif [ expression2 ] then # do something else else # do something more .... fi
We have understood how the conditions will look like in bash let us continue with the examples.
[[ ]] isn’t a normal if block it is utility which evaluates to either 0 or 1 based on expression.
CHECKING STRING EQUALITY
String comparison can be quite confusing even on other programming languages. Because there are so many implementations of same thing on multiple platforms. But here on bash it’s quite straight forward.
# STRING IS EMPTY
Let us see how we can check if a given string is empty. Remember the
-z which is used to check if the string is empty. And evaluates
true if it is.
# declaring and assigning nothing to the variable empty="" if [[ -z "$empty" ]]; then echo "String is empty." # or do what you like when the string is empty else echo "Not empty." # or do what you like when it is not fi;
And a simple output to the above code is provided below.
# STRING ISN’T EMPTY
Let us see how to check if a string is not empty. Remember the
-n option which will evaluate to true if the string is not empty.
# declaring and assigning nothing to the variable empty="" if [[ -n "$empty" ]]; then echo "Not empty." # or do what you like when the string is not empty else echo "String is empty." # or do what you like when it is fi;
The output above shows that the code has executed and printed out
"String is empty." because we have
empty="". The output below shows exactly that.
# EQUAL STRINGS
Let us see how we might compare two strings.
# declaring first string variable str1="techenum" # declaring second string variable str2="techenum" # comparing and printing out the result if [[ $str1 = $str2 ]]; then echo "Strings are equal." else echo "Strings are not equal." fi
In the code of block above if the
str2 are both same the
then block is executed or else the
else block is executed. Try changing the code and try executing it.
A sample output is provided below showing the proof of the code. The code in image above uses different strings but you get the point.
# NOT EQUAL STRINGS
The code below shows how to reverse the condition from above potion. It is exactly the same we are just flipping the conditions from. Check the code out:
# declaring first string variable str1="techenum" # declaring second string variable str2="techenum" # comparing and printing out the result if [[ $str1 != $str2 ]]; then echo "Strings are not equal." else echo "Strings are equal." fi
The code above when executed will produce the following result shown in the image below.
With the code above we have concluded about the bash programming conditionals for String. However, we have not yet looked at the number comparison. So let us continue …
Here we will look at conditionals in bash programming for numbers. We will learn to check if the numbers are equal, not equal, less than etc. So let us dive right in with the equal comparison at first.
The code is almost same as the one used in above examples. But regardless we will be doing a verbose coding for this portion as well.
# EQUAL NUMBERS
To check if the number are equal all you have to do is use the
-eq option in an expression. Check out the code below to understand what I mean by that.
num1=11 # first number num2=22 # second number if [[ $num1 -eq $num2 ]]; then echo "$num1 and $num2 are equal"; else echo "$num1 and $num2 are not equal"; fi;
If we run the block of code above we will have the following output.
Now let us move on to the next condition checking if they are not equal.
# UNEQUAL NUMBERS
The code is almost same but is slightly modified from above. Check out the
-ne option passed in the expression.
num1=11 # first number num2=22 # second number if [[ $num1 -ne $num2 ]]; then echo "$num1 and $num2 are not equal"; else echo "$num1 and $num2 are equal"; fi;
The output produced by the code above is shown in the image below. The output is exactly same because we have flipped condition but not the values in the variables.
# GREATER THAN
Now let us look at the exact same code for checking if either of the numbers are greater than the other. We will be using
-gt option. Use
-ge inplace of
-gt to check for
greater than or equal to.
num1=11 # first number num2=22 # second number if [[ $num1 -gt $num2 ]]; then echo "$num1 is greater than $num2."; else echo "$num1 is less than $num2."; fi;
The code above yields the output shown below.
# LESS THAN
Now let us try to flip the condition above with the option
-le only when you want to evaluate
less than or equal to. That being said let us evaluate the condition in code as:
num1=11 # first number num2=22 # second number if [[ $num1 -lt $num2 ]]; then echo "$num1 is less than $num2."; else echo "$num1 is greater than $num2."; fi;
The code above will produce the output shown in the image below.
Now with the example above we have successfully completed the very basics of bash programming for conditionals. This is just a simple example of bash what you want to do is up to you. You can create really creative scripts with bash programming. And toady you have learned a thing or two about the if else condition in bash programming.