Bash
Conditions
Conditional statements in Bash
Conditions
Conditional statements allow your scripts to make decisions based on conditions.
If Statement
# Basic if statement
if [ condition ]; then
echo "Condition is true"
fi
# If-else
if [ condition ]; then
echo "True"
else
echo "False"
fi
# If-else-if-else
if [ condition1 ]; then
echo "Condition 1 is true"
elif [ condition2 ]; then
echo "Condition 2 is true"
else
echo "All false"
fiTest Conditions
String Comparisons
# String equality
[ "$str1" = "$str2" ] # equal
[ "$str1" != "$str2" ] # not equal
[ -z "$str" ] # string is empty
[ -n "$str" ] # string is not emptyNumeric Comparisons
# Numeric comparisons
[ $num1 -eq $num2 ] # equal
[ $num1 -ne $num2 ] # not equal
[ $num1 -lt $num2 ] # less than
[ $num1 -le $num2 ] # less than or equal
[ $num1 -gt $num2 ] # greater than
[ $num1 -ge $num2 ] # greater than or equal
# Example
age=25
if [ $age -ge 18 ]; then
echo "You are an adult"
fiFile Tests
# File existence and type
[ -e $file ] # file exists
[ -f $file ] # regular file
[ -d $file ] # directory
[ -L $file ] # symbolic link
[ -p $file ] # named pipe
[ -S $file ] # socket
# File permissions
[ -r $file ] # readable
[ -w $file ] # writable
[ -x $file ] # executable
# File size
[ -s $file ] # file has size > 0
# Example
if [ -f "/etc/config" ]; then
echo "Config file exists"
fiLogical Operators
# AND operator
[ condition1 ] && [ condition2 ]
[ condition1 ] && [ condition2 ] && [ condition3 ]
# OR operator
[ condition1 ] || [ condition2 ]
[ condition1 ] || [ condition2 ] || [ condition3 ]
# NOT operator
[ ! condition ]
# Multiple conditions in single brackets
[ condition1 ] && [ condition2 ]
# Example
if [ -f "$file" ] && [ -r "$file" ]; then
echo "File exists and is readable"
fiTest Command Alternatives
# Using test command
test condition
[ condition ] # equivalent
[[ condition ]] # extended test (Bash only)
# Double brackets allow more features
[[ $string == pattern* ]] # pattern matching
[[ $num -eq 5 ]] # arithmeticCase Statement
# Switch/case statement
case $variable in
pattern1)
echo "Matches pattern1"
;;
pattern2|pattern3)
echo "Matches pattern2 or pattern3"
;;
*)
echo "Default case"
;;
esac
# Example
case $grade in
A)
echo "Excellent"
;;
B)
echo "Good"
;;
C)
echo "Average"
;;
*)
echo "Invalid grade"
;;
esacCommand Exit Status
# Check if command succeeded
if command; then
echo "Command succeeded"
else
echo "Command failed"
fi
# Exit status stored in $?
command
if [ $? -eq 0 ]; then
echo "Success"
else
echo "Failed with code $?"
fi
# Using && and ||
command && echo "Success" || echo "Failed"Practical Examples
#!/bin/bash
# Check argument count
if [ $# -lt 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
file=$1
# Validate file
if [ ! -f "$file" ]; then
echo "Error: File not found: $file"
exit 1
fi
# Check permissions
if [ ! -r "$file" ]; then
echo "Error: File not readable"
exit 1
fi
echo "File is valid and readable"Best Practices
- Always quote variables:
[ "$var" = "test" ] - Use
[[ ]]when available for better functionality - Check exit status for error handling
- Use descriptive variable names in conditions
- Comment complex conditions
- Validate input before use
- Handle edge cases (empty strings, missing files)