In this article, we’ll explore the various types of conditional statements in Bash and how to use them effectively.

If Statement

The most basic conditional statement is the if statement. It allows you to execute code blocks based on whether a condition is true or false.
Basic Syntax:

1
2
3
if [ condition ]; then
# commands if condition is true
fi

Example:
1
2
3
4
5
6
7
#!/bin/bash

age=25

if [ $age -ge 18 ]; then
echo "You are an adult."
fi

If-Else Statement

The if-else statement allows you to specify an alternative code block to execute when the condition is false.
Syntax:

1
2
3
4
5
if [ condition ]; then
# commands if condition is true
else
# commands if condition is false
fi

Example:
1
2
3
4
5
6
7
8
9
#!/bin/bash

temperature=28

if [ $temperature -gt 30 ]; then
echo "It's hot outside!"
else
echo "The weather is pleasant."
fi

If-Elif-Else Statement

For multiple conditions, use the elif (else if) clause to check additional conditions.
Syntax:

1
2
3
4
5
6
7
if [ condition1 ]; then
# commands for condition1
elif [ condition2 ]; then
# commands for condition2
else
# commands if no condition is true
fi

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

grade=75

if [ $grade -ge 90 ]; then
echo "A"
elif [ $grade -ge 80 ]; then
echo "B"
elif [ $grade -ge 70 ]; then
echo "C"
else
echo "F"
fi

Nested If Statements

You can nest if statements inside other if statements for more complex logic.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

age=25
has_license=true

if [ $age -ge 18 ]; then
if [ "$has_license" = true ]; then
echo "You can drive a car."
else
echo "You need a license to drive."
fi
else
echo "You're too young to drive."
fi

Case Statement

For multiple conditions with specific values, the case statement can be more readable than multiple if-elif statements.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

fruit="apple"

case $fruit in
"apple")
echo "It's an apple."
;;
"banana")
echo "It's a banana."
;;
*)
echo "Unknown fruit."
;;
esac

Logical Operators

Use logical operators to combine conditions:

  • AND: &&
  • OR: ||
  • NOT: !

Example:

1
2
3
4
5
6
7
8
#!/bin/bash

age=25
has_job=true

if [ $age -ge 18 ] && [ "$has_job" = true ]; then
echo "You're an employed adult."
fi

File Test Operators

Bash provides special operators for testing file attributes:

  • -e: file exists
  • -f: regular file
  • -d: directory
  • -r: readable
  • -w: writable
  • -x: executable

Example:

1
2
3
4
5
#!/bin/bash

if [ -f "myfile.txt" ]; then
echo "myfile.txt exists and is a regular file."
fi

Conclusion

Mastering conditional statements in Bash allows you to create more dynamic and responsive scripts. By combining these different types of conditionals and operators, you can handle complex decision-making processes in your Bash scripts efficiently.
Remember to test your conditions thoroughly and consider edge cases when writing conditional statements. With practice, you’ll become proficient in using these powerful constructs to enhance your Bash scripting skills.