Bash provides programmatic conditional statements. Conditionals provide a decision point for the application flow. For example, if the file exists to do this if not do another thing like logic operations.
if else Syntax
Bash if-else
statement can be used in 3 different cases. Here list of their syntax.
Single Case Check
We can check for a single case or situation with the if .. then .. fi
.
if CASE
then
COMMANDS
fi
- We will check the CASE with different logical operations like equal, greater then, same, etc. and if this case is true we will execute the COMMANDS, if not we will do not execute any commands.
Two Case Check
In two case check, we will check two cases. If the first given CASE is true we will execute COMMANDS1, if it is not true we will execute else COMMANDS2.
if CASE
then
COMMANDS1
else
COMMANDS2
fi
Multiple Case Check
In multiple case check, we will check the CASE. If it is not true CASE2 will be checked if not CASE3 will be checked. We can create a lot of cases with the elif
. When the case is true then its COMMAND will be executed. If all of the if
and elif
is false than the last part which is else
COMMANDSN will be executed.
if CASE
then
COMMANDS1
elif CASE2
COMMANDS2
elif CASE3
COMMANDS3
...
else
COMMANDSN
fi
As we see there is a different type of syntax of if statements. Actually, they are very similar to each other in logic. We will look at them in detail below.
Single Condition If
This type of if the syntax is very simple we will just check the single condition and then execute the required code.
#!/bin/bash
if true;
then
echo "TRUE";
fi;
This is a very simple example first we look if the line where it is true which means the condition is met so below then the branch will be executed. Then branch prints “TRUE” and fi ends if branch.
Two Condition With Else
We can improve our previous if condition and add a condition if the first condition is not met. Think this like true or false. For two situations there are two branches.
#!/bin/bash
myvalue=false
if $myvalue;
then
echo "TRUE";
else
echo "FALSE";
fi;
Multiple Condition If
Generally, there will be more than one condition. We can specify multiple conditions using multiple elif statements like below.
#!/bin/bash
age=12;
if [ $age -ge 18 ];
then
echo "Age is equal or greater than 18";
elif [ $age -ge 7 ];
then
echo "Age is between 7 and 17"
else
echo "Age is below 7";
fi;
In this example, we have three cases where first is equal or greater than 18 with the if line. Second is the elif line and looks equal to or greater than 7. The third one is else statement which is logically below 7.
-ge
is used as greater or equal operations.
Square brackets [
, ]
are used to output the result of the compare operation inside it to the condition.
For more information about the logic operations like equal, greater than, etc. look following tutorial.
Linux Bash Operators Like Assignment, Plus, Calculation
Comparison and Check Operators
During the usage of the if-else
we generally need to check cases by using comparison and check operators. They can be used to check if a file exists or a given number is greater than the other. Here we will list some of the most useful comparisons and check operators which can be used for if-else-elif
cases.
True
true
is a logical value that specifies the positive case. true
will match the case in if-else-elif
.
true
False
false
is a logical value that specifies the negative case. false
will not match the case in if-else-elif
.
Negative EXPRESSION
While using if-elif-else
we will use logical true and false. We can revert given logic back by prefixing the logic value with !
. If the given value is true and prefixed with !
it will be false. If the given value is false and prefixed with !
it will be true.
!true
is equal to the false!false
is equal to the true
STRING Is Greater Than Zero
We can check the size or character count of the STRING has more than 0 characters and it is not empty.
-n STRING
STRING Is Zero
We can check the size or character count of the STRING has 0 characters and it is empty.
-z STRING
STRING1 Is Equal To STRING2
One of the most used comparisons and check operator is checking if two given string is equal or the same. We can use an equal sign =
like below.
STRING1 = STRING2
STRING1 Is Not Equal To STRING2
We can check if given STRING1 is not equal to another STRING2 with the !=
.
STRING1 != STRING2
INTEGER1 Is Equal To INTEGER2
Another useful and popular comparison and check operation are checking if two given integer is equal numerically. We can use -eq
operator like below.
INTEGER1 -eq INTEGER2
INTEGER1 Is Greater Than INTEGER2
Another useful and popular comparison and check operation are checking if INTEGER1 is greater than INTEGER2 numerically. We can use -gt
operator like below.
INTEGER1 -gt INTEGER2
INTEGER1 Is Greater Than Or Equal To INTEGER2
Another useful and popular comparison and check operation are checking if INTEGER1 is greater than or equal to INTEGER2 numerically. We can use -ge
operator like below.
INTEGER1 -ge INTEGER2
INTEGER1 Is Less Than INTEGER2
Another useful and popular comparison and check operation are checking if INTEGER1 is less than INTEGER2 numerically. We can use -lt
operator like below.
INTEGER1 -lt INTEGER2
INTEGER1 Is Less Than Or Equal To INTEGER2
Another useful and popular comparison and check operation are checking if INTEGER1 is less than or equal to INTEGER2 numerically. We can use -le
operator like below.
INTEGER1 -le INTEGER2
FILE Exists and Is A Directory
We can also check some file and directory properties. We can check if the given file is a directory and exists with the -d
operator.
-d FILE
FILE Exists
We can only check if a given file or directory exist with the -e
operator like below.
-e FILE
FILE Exists and Size Is Greater Than Zero
We can check if the given file exists and there is some content inside it which means it is not empty and size is greater then zero.
-s FILE
Use If Else In A Single Line
In some cases, we may need some clarity and readability. So we should use If Else
in a single line which is valid as far as we obey the syntax. We can express if else like below. We should be aware that the spaces before and after if
, then
, else
and fi
are important.
#!/bin/bash
myvalue=false
if $myvalue; then echo "TRUE"; else echo "FALSE"; fi;
Nested If Else
We may a complex situation where nested logic exists. We can use If Else
in a nested manner to solve or process this case. In this example, we first check myvalue
variable whether it is True
or False
. If it is True we enter nested if-elif-else
. If not True we execute else
which prints FAILURE
. If it is True then we check myothervalue
whether it is 1
.If it is 1 then we print OK
if not we print nothing.
#!/bin/bash
myvalue=false
myothervalue=1
if $myvalue; then
if $myothervalue -eq 1; then
echo "OK"
fi;
else
echo "FAILURE";
fi;
Check If A File Exist
I think one of the most wanted examples is to check if a file exists and execute code according to the result. We will use -a
operator which is used to check folder existence and returns true if the given file exists.
#!/bin/bash
if [ -a "/home" ]
then
echo "/home directory exists."
fi

Check If A Softlink Exist
We can check if the provided file exists and the symbolic link. We will use the -h
option which will check for the given file is a soft link or not.
#!/bin/bash
if [ -h "/bin/ping4" ];
then
echo "/bin/ping4 exist and symbolic link";
fi;

Compare Numbers and Check If A Is Lower Than B
Another useful if-else check is comparing numbers. We can check two numbers and find if A is lower or equal to B.
#!/bin/bash
a=12
b=15
if [ $a -le $b ]
then
echo "$a is lower or equal to $b"
fi

We compare two variables named $a
and $b
with -le
with means lower or equal. If compare returns true We print the message to the console.
String Compare
String variables can be compared too. For example, we may want to check the current user and compare if it is root.
#!/bin/bash
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi
We get the current user name with whoami
command and compare it if it is different from “root” with !=
. If the username is different than root we print a message to the console.
Check If A Variable Is Defined
It is very useful to check if a variable is set in the script file.
#!/bin/bash
if [ -z ${HOME+x} ];
then
echo "HOME is unset";
else
echo "HOME is set to '$HOME'";
fi