Linux Tactic

Mastering Bash Conditional Statements: If If-else and If-elif-else

Bash is a popular Unix shell and command language that is widely used for scripting purposes. It offers a wide range of features, including conditional statements, which enable you to execute speci

fic commands based on de

fined conditions. In this article, we’ll explore the basics of Bash conditional statements and explain how they work.

Basics of Conditional Statements

Conditional statements, also known as control structures, are essential in programming as they enable you to control the flow of execution of a program. You can use them to perform tests and make decisions based on the outcome of the tests.

The most common type of conditional statement is the Boolean condition. In Bash, Boolean conditions can take the form of true/false, 0/1, or on/off.

If Statement

The if statement is the most basic conditional statement in Bash. It is used to execute a block of code only if a speci

fied condition is true. The general syntax of the if statement is:

if [condition]

then

[commands]

fi

The square brackets in the condition are known as test brackets and are used to evaluate the condition. The commands inside the if block are executed if the condition is true.

If-

else Statement

The if-

else statement is used to execute one block of code if a condition is true, and another block if the condition is false. The general syntax of the if-

else statement is:

if [condition]

then

[commands if condition is true]

else

[commands if condition is false]

fi

The commands inside the

else block are executed if the condition is false. If-elif-

else statement

The if-elif-

else statement is used to execute one block of code from multiple blocks, based on different conditions. The general syntax of the if-elif-

else statement is:

if [condition1]

then

[commands if condition1 is true]

elif [condition2]

then

[commands if condition2 is true]

else

[commands if all conditions are false]

fi

The elif statement is used to evaluate additional conditions. The commands inside the

else block are executed if all conditions are false. Syntax of

If Statement

The syntax of the if statement consists of a conditional expression followed by the

then keyword and a block of commands. The general syntax is:

if [conditional expression]

then

[commands]

fi

The conditional expression can be any valid Bash expression that returns a value of true or false. The

then keyword indicates the start of the block of commands to be executed if the condition is true. The

fi keyword marks the end of the if statement. Working of

If Statement

The if statement evaluates the conditional expression and executes the commands if the expression is true.

If the expression is false, the commands are skipped. The evaluation of the conditional expression follows the rules of arithmetic evaluation.

For example, the expression 10 -gt 5 returns a value of 1, which is true in Bash.

If Statement Example

Here is an example of the if statement that checks if a number is greater than 10:

#!/bin/bash

echo “Enter a number: “

read num

if [ $num -gt 10 ]

then

echo “The number is greater than 10”

fi

In this example, the read command prompts the user to enter a number, which is stored in the variable num. The if statement checks if the value of num is greater than 10 using the -gt operator.

If the condition is true, the echo command displays a message.

Conclusion

In this article, we’ve explained the basics of Bash conditional statements, including the if, if-

else, and if-elif-

else statements. We’ve also discussed the syntax and working of the if statement and provided an example to demonstrate its usage.

By mastering conditional statements, you can take your Bash scripting to the next level and create more powerful and flexible scripts. Bash

If-

else Statement

The Bash

If-

else Statement is the second most basic conditional statement in Bash that is executed after the if statement. The if-

else statement is used when there is a requirement to execute one block of code when the condition evaluates as true and a different block of code when the condition evaluates as false.

Syntax of If-

else statement

The syntax of the if-

else statement is similar to the if statement, with a small addition of the

else keyword to introduce a separate block of code that will be executed when the condition returns false. The general syntax is:

if [conditional expression]

then

[commands 1]

else

[commands 2]

fi

Here, if the conditional expression returns true,

then the code inside the ‘

then’ block is executed. If the conditional expression returns false,

then the code inside the ‘

else’ block is executed. Working of If-

else statement

The

If-

else Statement checks whether a condition is true or false, and based on the result, either the

first code block or the second code block is executed. The conditional expression can be any valid Bash expression that returns a value of true or false.

The working of the if-

else statement is as follows:

1. The Bash Interpreter evaluates the condition in the square brackets [ ], which is part of the

If-

else Statement. 2.

If the condition is true,

then the code inside the ‘

then’ block is executed, and the code inside the ‘

else’ block is ignored. 3.

If the condition is false,

then the code inside the ‘

else’ block is executed, and the code inside the ‘

then’ block is ignored. If-

else statement example

Here is an example of the If-

else statement that checks whether a number is greater than 10 or less than or equal to 10. #!/bin/bash

echo “Enter a number: “

read num

if [ $num -gt 10 ]

then

echo “The number is greater than 10”

else

echo “The number is less than or equal to 10”

fi

In this example, the read command prompts the user to enter a number, which is stored in the variable ‘num’. The

If-

else Statement is used to check the condition in square brackets. If the entered number is greater than 10,

then the code inside the ‘

then’ block is executed and displays a message. If the number is less than or equal to 10,

then the code inside the ‘

else’ block is executed and displays a different message. Bash If-elif-

else Statement

The Bash If-elif-

else Statement is used when there is a requirement to evaluate multiple conditions. The If-elif-

else Statement allows multiple conditions to be tested sequentially. If any of the conditions are true,

then the corresponding code block is executed, and the rest are skipped.

Syntax of If-elif-

else statement

The syntax of the If-elif-

else statement is similar to the If-

else statement, with a small addition of the elif keyword to allow multiple conditions to be tested. The general syntax is:

if [condition1]

then

[commands 1]

elif [condition2]

then

[commands 2]

else

[commands 3]

fi

Here, if condition1 evaluates as true,

then the code inside the ‘

then’ block is executed, and the code inside ‘elif’ and ‘

else’ blocks is skipped. If condition1 evaluates as false, the interpreter evaluates condition2 and proceeds with either the code inside the ‘

then’ block or the ‘

else’ block accordingly. Working of If-elif-

else statement

The If-elif-

else Statement checks multiple conditions systematically. Unlike a series of

If-

else Statements, the If-elif-

else Statement executes only one code block. When a condition evaluates as true, the code block corresponding to that condition is executed, and the interpreter skips all other conditions.

If-elif-

else statement example

Here is an example of the If-elif-

else statement that checks whether a number is greater than 10, less than 10, or equal to 10. #!/bin/bash

echo “Enter a number: “

read num

if [ $num -gt 10 ]

then

echo “The number is greater than 10”

elif [ $num -lt 10 ]

then

echo “The number is less than 10”

else

echo “The number is equal to 10”

fi

In this example, the read command prompts the user to enter a number, which is stored in the variable ‘num’. The If-elif-

else Statement is used to check three different conditions using ‘-gt’ (greater than), ‘-lt’ (less than), and ‘-eq’ (equal to) operators. If the entered number is greater than 10,

then the code inside the ‘

then’ block is executed and displays a message. If the number is less than 10,

then the code inside the ‘elif’ block is executed and displays a different message. If the number is equal to 10,

then the code inside the ‘

else’ block is executed and displays a different message.

Conclusion

Bash

If-

else Statements and Bash If-elif-

else Statements are powerful conditional statements that greatly enhance the functionality and usability of Bash scripts. With some practice and creativity, one can create scripts of great complexity and ef

ficiency using these statements. In this article, we have covered the syntax, working, and implementation of If-

else and If-elif-

else Statements, and we hope this knowledge will be useful in your future Bash scripting endeavors. In this article, we explored the basics of Bash conditional statements, including If, If-

else, and If-elif-

else statements. We discussed their syntax and working and provided examples to demonstrate their usage.

By mastering these conditional statements, you can take your Bash scripting to the next level and create more powerful and flexible scripts. These statements are essential in programming as they enable you to control the flow of execution of a program.

We hope this knowledge will be useful in your future Bash scripting endeavors and help you create ef

ficient and complex scripts.

Popular Posts