Conditional Statements in Shell Scripting
Conditional statements in shell scripting help in decision-making based on
conditions. These statements execute different blocks of code depending on
whether a condition is true or false.
1. if Statement
The if statement executes a block of code only if the given condition is true.
Syntax:
if [ condition ]
then
# Code to execute if condition is true
fi
Example:
#!/bin/bash
num=10
if [ $num -gt 5 ]
then
echo "The number is greater than 5"
fi
Explanation:
• The condition [ $num -gt 5 ] checks if num is greater than 5.
• Since 10 > 5, it prints "The number is greater than 5".
2. if-else Statement
The if-else statement executes one block if the condition is true, otherwise, it
executes another block.
Syntax:
if [ condition ]
then
# Code if condition is true
else
# Code if condition is false
fi
Example:
#!/bin/bash
num=3
if [ $num -gt 5 ]
then
echo "The number is greater than 5"
else
echo "The number is not greater than 5"
fi
Explanation:
• Here, num=3, so [ 3 -gt 5 ] is false.
• The script executes the else block and prints "The number is not greater
than 5".
3. if-elif-else Statement
The if-elif-else statement is used when multiple conditions need to be
checked.
Syntax:
if [ condition1 ]
then
# Code if condition1 is true
elif [ condition2 ]
then
# Code if condition2 is true
else
# Code if none of the conditions are true
fi
Example:
#!/bin/bash
num=0
if [ $num -gt 0 ]
then
echo "The number is positive"
elif [ $num -lt 0 ]
then
echo "The number is negative"
else
echo "The number is zero"
fi
Explanation:
• If num is greater than 0, it prints "The number is positive".
• If num is less than 0, it prints "The number is negative".
• If neither condition is true, it prints "The number is zero".
4. Nested if Statement
A nested if is an if statement inside another if statement.
Syntax:
if [ condition1 ]
then
if [ condition2 ]
then
# Code if both conditions are true
fi
fi
Example:
#!/bin/bash
num=15
if [ $num -gt 10 ]
then
echo "Number is greater than 10"
if [ $num -lt 20 ]
then
echo "Number is also less than 20"
fi
fi
Explanation:
• First if checks if num > 10, which is true.
• Then, inside it, another if checks if num < 20, which is also true.
• It prints:
Number is greater than 10
Number is also less than 20
5. Case Statement
The case statement is used when there are multiple options to choose from.
Syntax:
case variable in
pattern1) command ;;
pattern2) command ;;
*) default command ;;
esac
Example:
#!/bin/bash
echo "Enter a fruit:"
read fruit
case $fruit in
"apple") echo "Apple is red" ;;
"banana") echo "Banana is yellow" ;;
"grape") echo "Grape is purple" ;;
*) echo "Unknown fruit" ;;
esac
Explanation:
• If the user enters "apple", it prints "Apple is red".
• If the user enters "banana", it prints "Banana is yellow".
• If the input doesn’t match any case, it prints "Unknown fruit".
Key Takeaways
Conditional statements help in decision-making in shell scripting.
The if statement executes a block if a condition is true.
The if-else statement provides an alternative block if the condition is false.
The if-elif-else statement allows multiple conditions.
Nested if helps in checking conditions inside another condition.
Case statement is useful for checking multiple values efficiently.