Bash: If, Else If, Else Examples – Linux Hint

In this Linux Hint tutorial we will teach Bash scripting conditionals using IF, ELIF and ELSE commands. The IF condition is the most basic way to take a conditional action in a bash script, so it’s important to have a firm understanding of its syntax and options to be productive. When using IF conditions in Bash, you can optionally enter ELIF conditions that will provide different actions depending on which of a group of conditions is TRUE. In addition, the ELSE condition can optionally be included, which will provide code to run if none of the conditions in the group are met.

Specific examples shown in this article include

: Example 1: If statement in bash on string equality Example 2: If statement on bash on

  1. equality of numbers
  2. Example 3: If bash instruction on

  3. less than one number
  4. Example 4: If statement on bash with logical operator AND Example 5:

  5. If statement on bash with logical OR operator Example
  6. 6

  7. :
  8. Elif Condition
  9. in Bash

  10. Example 7

: If Else Statement in Bash Scripting

General Bash

If ELIF Else Syntax The following is the general syntax of IF ELIF and ELSE in bash:

Note: In the general syntax above, you can include zero, one, or more ELIF conditions in the code block, and the ELSE condition is optional.

The construction of the actual conditions has many options because the bash syntax has many different features and options, but a very basic template for your bash scripts would be to use the syntax [[ double brack as shown in the general syntax example and specific examples below.

Note also: That depending on the formatting aesthetics of your code you may like the then on the same line of conditions or you can choose to put on the following line as shown below:

Now let’s show specific examples of the basic variations of the IF ELIF and ELSE conditions in the working examples below

.

Example 1: If statement in bash about

string equality

In this example above, we use the basic IF condition with a single string equality test and then print some output in case the condition is true based on user input. Note that you can place the THEN statement on the next line if you prefer that style. See the example output below

:

Example 2: If statement in bash over

numerical equality

In this example above, we again make a simple IF condition, but in this case we demonstrate how to do a test comparison in numerical values. You can see the result below with the matching and mismatched

conditions:

Example 3: If the instruction in bash in less than one number

In addition to –eq for equality, you can also test the test conditions -lt -le –gt -ge for less than, less or equal, greater than or equal, respectively. The following is a similar example with less than the -lt operator

:

The output of this script will be able to test if the input is less than 100 and execute specific code in that

condition:

Example 4: If statement on bash

with logical operator AND

This example will show how to combine multiple conditions with a logical AND condition. You can also use logical OR conditions in IF instruction constructs. The logical code AND in bash is written with double ampersand &&. The following is an example of AND logic in a bash IF condition: We will

test this with several outputs to show that only if the first and second conditions are true, will the output be printed

:

Example 5: If the bash instruction with the logical operator

OR

Logical OR in bash scripts is written with double vertical slashes || . Let’s try the previous example again with a slight variation to try an example of

the logical OR operator:

In this example, the logical OR operator combines two conditions and only one or both of them can be true so that the condition is true general and the code executes. We also proved the -ge operator for a greater or equal numerical comparison in the example above. The script works as expected in the following output

:

Example 6: elif condition in bash

Now we will provide an example for multiple if conditions in the code block with ELIF. You can have zero ELIF conditions, one ELIF conditions, or multiply in a code block. For this example, we will provide two ELIF conditions as a demonstration, but you can use any number of ELIF blocks in practice.

We will test this code with a variety of numbers to demonstrate which if or ELIF condition fires

:

Example 7: If Else Statement in Bash Scripting

Now we will demonstrate the ELSE condition. The ELSE condition is used in the event that the IF condition or all ELIF conditions do not exist, the ELSE condition will be used, and the ELSE condition code will be executed. Let’s add an ELSE condition example to the previous example, but to be clear, ELIF is not required to take advantage of the ELSE condition. The ELSE condition can be used with an IF condition and no ELIF condition.

Let’s test the example code with a simple test case that should fall into the ELSE condition:

CONCLUSION

We have seen how to use IF, ELIF, and ELSE conditions to form conditional execution in many combinations in bash scripts and bash programs. For the next steps, you can study the test conditions and the variations and varieties available to test a condition. You can also consider the bash case statement as an alternative to conditional logic in bash scripts. Finally, the GNU Bash Reference Manual is a good source of truth when looking for exact syntax details.

Contact US