Javatpoint Logo
Javatpoint Logo

Bash If

In this topic, we will understand how to use if statements in Bash scripts to get our automated tasks completed.

Bash if statements are beneficial. They are used to perform conditional tasks in the sequential flow of execution of statements. If statements usually allow us to make decisions in our Bash scripts. They help us to decide whether or not to run a piece of codes based upon the condition that we may set.

Basic if Statements

A basic if statement commands that if a particular condition is true, then only execute a given set of actions. If it is not true, then do not execute those actions. If statement is based on the following format:

Syntax

The statement between then and fi (If backwards) will be executed only if the expression (between the square brackets) is true.

Note: Observe the spaces used in the first line and a semicolon at the end of the first line; both are mandatory to use. If conditional statement ends with fi.

  • For using multiple conditions with AND operator:
  • For using multiple conditions with OR operator:
  • For compound expressions with AND & OR operators, we can use the following syntax:

Following are some examples demonstrating the usage of if statement:

Example 1

In this example, take a user-input of any number and check if the value is greater than 125.

Output

If we enter the number 159, then the output will look like:

Bash If Statement

Example 2

In this example, we demonstrate the usage of if statement with a simple scenario of comparing two strings:

Output

Bash If Statement

Example 3

In this example, we demonstrate how to compare numbers by using the if statement:

Output

Bash If Statement

Example 4

In this example, we will define how to use AND operator to include multiple conditions in the if expression:

Output

Bash If Statement

Example 5

In this example, we will define how to use OR operator to include multiple conditions in the if expression:

Output

Bash If Statement

Example 6

In this example, we will define how to use AND and OR to include multiple conditions in the if expression:

Output

Bash If Statement

Options for If statement in Bash Scripting

If statement contains many options to perform a specific task. These options can be used for file operations, string operations, etc. Following are the some mostly used options:

-
Options (Operators) Description
! EXPRESSION To check if EXPRESSION is false.
-n STRING To check if the length of STRING is greater than zero.
-z STRING To check if the length of STRING is zero (i.e., it is empty)
STRING1 == STRING2 To check if STRING1 is equal to STRING2.
STRING1 != STRING2 To check if STRING1 is not equal to STRING2.
INTEGER1 -eq INTEGER2 To check if INTEGER1 is numerically equal to INTEGER2.
INTEGER1 -gt INTEGER2 To check if INTEGER1 is numerically greater than INTEGER2.
INTEGER1 -lt INTEGER2 To check if INTEGER1 is numerically less than INTEGER2.
-d FILE To check if FILE exists and it is a directory.
-e FILE To check if FILE exists.
-r FILE To check if FILE exists and the read permission is granted.
-s FILE To check if FILE exists and its size is greater than zero (which means that it is not empty).
-w FILE To check if FILE exists and the write permission is granted.
x FILE To check if FILE exists and the execute permission is granted.

Nested If

You can apply as many 'if statements' as required inside your bash script. It is also possible to use an if statement inside another 'if statement'. It is known as Nested If Statement.

Example

In this example, we will find "if a given number is greater than 50 and if it is an even number" by using nested if expression.

Output

If we input an argument value as 100, then the output will look like:

Bash If Statement

Conclusion

In this topic, we discussed how we could use conditional branching in the sequential flow of execution of statements with bash if statement.






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA