Linux Bash IF – ELIF – ELSE Conditionals Tutorial with Examples – POFTUT

Linux Bash IF – ELIF – ELSE Conditionals Tutorial with Examples

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.

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.

  • 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 .

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 then the last part which is else COMMANDSN  will be executed.

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.

Single Condition If

Single Condition If

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.

Two Condition With Else

Two Condition With Else

Multiple Condition If

Generally, there will be more than one condition. We can specify multiple conditions using multiple elif statements like below.

LEARN MORE  Awk If, If Else , Else Statement or Conditional Statements

Multiple Condition If

Multiple Condition If

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.

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.

STRING Is Zero

We can check the size or character count of the STRING has  0 characters and it is empty.

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.

LEARN MORE  What Is If Statement and Use In JavaScript, PHP, Python, Java, C/C++, C#, PowerShell, Bash Programming Languages

STRING1 Is Not Equal To STRING2

We can check if given STRING1 is not equal to another STRING2 with the !=.

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 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 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 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 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.

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.

FILE Exists

We can only check if a given file or directory exist with the -e operator like below.

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.

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.

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.

LEARN MORE  Linux Bash Case Statement with Examples

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.

Check If A File Exist

Check If A File Exist

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.

Check If A Softlink Exist

Check If A Softlink Exist

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.

Compare Numbers and Check If A Is Lower Than B

Compare Numbers and Check If A Is Lower Than B

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.

String Compare

String Compare

We get the current user name with whoami command and compare 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.

Check If A Variable Is Defined

Check If A Variable Is Defined

Leave a Reply

Your email address will not be published. Required fields are marked *