
The if-else statement in Bash is a conditional statement that executes specific commands depending on the results of an evaluation. The statement evaluates expressions to check whether a condition returns true or false. The if statement runs when a condition returns true and the else statement runs when the condition returns false.
This article explains how to use the if-else statement in Bash to test conditions and execute specific commands when the result is true or false.
The if statement evaluates the result of a condition and executes the respective commands when the condition evaluates to true. The statement has the following components:
In the above code:
if: Defines the condition that you want to evaluate.then: Defines the commands to execute if the condition returns true.fi: Defines the end of the statement.Example:
Output:
The above script evaluates the equality of numeric values and returns the result 1 is equal to 1 if the condition returns true.
The If-else statement evaluates a condition as true or false and executes the if part when true and the else part when false. The statement executes alternative commands if a condition does not match the if block. The following is the if-else statement syntax.
For example:
Output:
In the above script, the if-else statement executes different commands depending on whether the condition is true or false. The else block executes alternative commands if the condition is false, while the if block executes commands if the condition is true.
If-else statements use conditional expressions to evaluate the result of a condition. You can replace the test operator with [ or [[ to evaluate a condition and execute specific commands. Follow the steps below to create a bash shell If else statement to test a condition and execute specific commands.
Create an If-else statement to test whether a number is greater than or equal to another number.
Output:
Create an If-else statement that checks if a data.txt file exists and creates it if it does not exist.
Output:
Create another If-else statement that compares the values of two strings.
Output:
Variables store data and values in Bash for flexible operations. You can combine If-else statements with variables to evaluate conditions and manipulate variable data based on different results.
Create a new auth_users variable and set the value to admin. Then, add a new read statement to capture a user's input to a new user variable. Declare an If-else statement that compares the user input variable to auth_users to evaluate whether the condition is true or false and run specific commands.
Output:
The above script runs the If-else statement and displays the You are an administrative user authorized to manage this system message if the condition returns true. If the condition is false, the script displays You are a standard user, you can run basic commands on the system message.
If-else-if (If-elif-else) is a conditional structure that compares multiple conditions to filter and execute matching commands. The following is the If-elif-else command syntax.
Change the previous if-else statement to include a new elif statement and output another result if the user input is empty.
Output:
The above script runs the if block when the condition is true. If the user input is empty, the elif block queries for a new variable value while else displays a You are a standard user, you can run basic commands on the system message when the condition is false.
You can use multiple conditions in bash If-else statements using logical operators such as AND and OR to evaluate two or more commands in one condition. This approach is important when the second command depends on the success or failure of the first command.
For example:
Output:
In the above script, the AND operator (&&) tests if the first command is successful before executing the second command.
Nested if-else statements allow you to place one if-else statement inside another. This structure is useful when evaluating multiple conditions to meet a specific result to allow further decision-making.
For example:
Output:
The above script uses a nested If-else statement in which the inner condition executes only if the outer condition triggers the else block.
You have used the If-else statement in Bash to create and test conditions. If-else is an important conditional statement that evaluates conditions as true or false before executing a set of instructions. You can also use If-else with other Bash components, such as loops to create advanced scripts.
0 Comments
Be the first to comment and share your perspective with the community.