What is Bash if statement?

The if is one of the decision making statement in Bash Shell scripting. It enables us executing one block of code (to perform certain action) among the two or more blocks.

As such, for taking a decision in any matter, there must be two or more options; so this is where if statement is for in computer programming.

Types of if statements in Bash Shell with examples

Depending on the scenario, you may use a different form of the if statement in Bash Shell scripting.

For example, if you want to check certain condition is true and not required to perform any action, task or display a simple message if the condition is false then you may use the simplest form of the if statement as follows:

There,

  • The if keyword is followed by the condition you want to check.
  • This is followed by “then” keyword.
  • After that, commands/statements to execute if that condition is true.
  • Finally, the “fi” keyword. Yes, this is not misspelled.

The example of simple if statement

In the example of showing how simple if statement works, we declared two variables and assigned them values.

In the if statement, we tested a condition whether two variable values are equal or not. If the condition is True, it will display a message, otherwise, no message is displayed or action is performed. Have a look:

The code:

The result:

The values of both variables are equal!

As you can see, the values of variables x and y are 5, so the condition is true and a statement in the if statement displayed.

The following code will display nothing as the condition is False:

How to use Bash else with if statement?

You saw in the above example, no action is performed or any statement displayed when the condition was false.

In certain scenarios, you will require doing something; for example showing a message box to the user or action failure, display a message or perform some default action if the condition is false.

The general way of using the Bash else statement is as follows:

An example of if..then..else.fi

See the example below with the output to learn how to use the if..then..else..fi in Bash scripting:

The code:

The result:

Values are not equal

You saw, as the condition is false, the statement inside the else statement executed.

Using the Bash elif statement

In many other languages, the elif statement is written as “elseif” or “else if”. The Bash elif statement enables us deciding from more choices than two; as we have seen in the above case.

For example, if a user enters the marks 90 or more, we want to display “Excellent”. For value more than or equal to 80 and less than 90, the displayed message is “Very Good”.

Similarly, for value more than or equal to 70 and less than 80, the displayed message should be “Good”. For less than 70, the message should be “Normal”.

So, how we can implement this by using the if statement. As such, we have seen the simple if statement allows us testing the single condition only. The else statement allows us performing an action if the if condition is False.

The answer is using the elif statement.

The example below shows using the elif statement. There, we have a variable which value is tested in the if, elif statements and if all are False, the else part should execute and display a message. Have a look:

The result:

The value is equal to 15

Similarly, the above scenario of marks can be translated into the if..elif..else statements as follows:

The output:

Good

In the example above, you can see how the AND (&&) operator is used to get the result along with testing multiple conditions in the if statement.