10+ Bash: If, Else If, Else Examples

10+ Bash: If, Else If, Else Examples

Bash: If, Else If, Else Examples

In this Bash tutorial post, we will teach you bash script with the IF, ELIF, and ELSE commands with 10+ examples.

10+ Bash: If, Else If, Else Examples

Here are the 10 bash examples:

1. Simple If Statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
fi

2. If-else statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is less than or equal to 10"
fi

3. If-else-if statement in bash:

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
elif [ $num -eq 10 ]; then
echo "Number is equal to 10"
else
echo "Number is less than 10"
fi

4. Nested if statement in bash:

if [ $num -gt 10 ]; then
if [ $num -lt 20 ]; then
echo "Number is between 10 and 20"
fi
fi

5. Using logical operators in bash:

if [ $num -gt 10 ] && [ $num -lt 20 ]; then
echo "Number is between 10 and 20"
fi

6. Checking file existence in bash:

if [ -e /path/to/file ]; then
echo "File exists"
else
echo "File does not exist"
fi

7. Checking if a variable is empty in bash:

if [ -z "$var" ]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi

8. Using a case statement in bash:

case $fruit in
apple)
echo "Fruit is apple"
;;
orange)
echo "Fruit is orange"
;;
*)
echo "Fruit is not apple or orange"
;;
esac

9. Using command substitution in bash:

if [ $(ls /path/to/dir | wc -l) -gt 0 ]; then
echo "Directory is not empty"
else
echo "Directory is empty"
fi

10. Using arithmetic expressions in bash:

if (( $num > 10 )); then
echo "Number is greater than 10"
elif (( $num == 10 )); then
echo "Number is equal to 10"
else
echo "Number is less than 10"
fi

3 examples of If statement in bash on string equality

Using double brackets and == operator:

READ More Relevant Stuff:  Install WhatsApp Client On Ubuntu 22.04 LTS/ Ubuntu 20.04 LTS

In this example, we use double brackets and the == operator to test for string equality. The advantage of using double brackets is that it is more flexible and can handle more complex conditions.

if [[ "$str1" == "$str2" ]]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi

Using the test command and = operator: 

In this example, we are using the test command with the = operator to test for string equality. The test command is equivalent to the [ command, but requires a trailing semicolon or space before the closing bracket.

if test "$str1" = "$str2"; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi

Using the case statement:

In the below example, we are using a case statement to check if $str1 matches $str2. If the two strings match, the “Strings are equal” message is printed. Otherwise, You will see the “Strings are not equal” message in output. The case the statement allows for more complex matching patterns and can be useful in situations where multiple conditions need to be checked.

case "$str1" in
"$str2")
echo "Strings are equal"
;;
*)
echo "Strings are not equal"
;;
esac

3 examples of If statement in bash on number equality

Using double brackets and == operator:

if [[ "$num1" == "$num2" ]]; then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

Using the test command and -eq operator:

if test "$num1" -eq "$num2"; then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

Using arithmetic expression:

if (( num1 == num2 )); then
echo "Numbers are equal"
else
echo "Numbers are not equal"
fi

3 example of If statement in bash on less than a number

Using double brackets and -lt operator:

if [[ "$num1" -lt "$num2" ]]; then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

Using the test command and -lt operator:

if test "$num1" -lt "$num2"; then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

Using arithmetic expression:

if (( num1 < num2 )); then
echo "$num1 is less than $num2"
else
echo "$num1 is not less than $num2"
fi

If statement in bash with logical AND operator

In this example, we are using the logical AND operator && to test if $num1 is greater than 0 AND less than 10. The -gt and -lt operators are used for numeric comparison. If both conditions are true, the message “$num1 is between 0 and 10” is printed. Otherwise, the message “$num1 is not between 0 and 10” will be printed. Note that we are using the separate brackets [ ] for each condition that we want to evaluate.

if [ "$num1" -gt 0 ] && [ "$num1" -lt 10 ]; then
echo "$num1 is between 0 and 10"
else
echo "$num1 is not between 0 and 10"
fi

If statement in bash with logical OR operator

In this example, we use the logical OR operator || to test if $day is equal to “Sunday” OR “Saturday”. If either condition is true, the message ” Yay, It’s the weekend!” is printed. Otherwise, the message “Sadly, It’s a weekday” is printed. Note that we use separate brackets [ ] for each condition that we want to evaluate.

if [ "$day" = "Saturday" ] || [ "$day" = "Sunday" ]; then
echo "Yay, It's the weekend!"
else
echo "Sadly, It's a weekday"
fi

3 Examples of elif condition in bash

Using elif to test for multiple conditions: 

if [ "$num" -gt 0 ]; then
echo "$num is positive"
elif [ "$num" -lt 0 ]; then
echo "$num is negative"
else
echo "$num is zero"
fi

Using elif with a different operator: 

READ More Relevant Stuff:  Install Synaptic Package Manager On Ubuntu 20.04 LTS

In this example, we are using an if statement to test if $user is not equal to “root”. If the condition is true, the message “You do not have root access” is printed.

If the condition is false, then the  elif statement tests if $user is equal to “root”. If this condition is true, the message “You have root access” is printed.

Note that we are using the != and = operators for string comparison.

if [ "$user" != "root" ]; then
echo "You do not have root access"
elif [ "$user" = "root" ]; then
echo "You have root access"
fi

Using elif with a nested if statement: 

In this example, we are using a if statement to test if $day is equal to “Saturday”. If the condition is true, the message “yay, It’s the weekend!” is printed.

If the condition is false, the elif statement tests if $day is equal to “Sunday”. If this condition is true, the message ” Yay, It’s the weekend!” is printed.

If both conditions are false, the else statement executes a nested if statement that tests if $hour is less than 12. If this condition is true, the message “Good morning!” is printed. If the condition is false, the message “Good afternoon!” is printed.

if [ "$day" = "Saturday" ]; then
echo "Yay, It's the weekend!"
elif [ "$day" = "Sunday" ]; then
echo "Yay, It's the weekend!"
else
if [ "$hour" -lt 12 ]; then
echo "Good morning!"
else
echo "Good afternoon!"
fi
fi

 

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *