if [ condition ]; then
# Code to execute if condition is true
fi
if [ condition ]; then
# Code to execute if condition is true
else
# Code to execute if condition is false
fi
if [ condition1 ]; then
# Code to execute if condition1 is true
elif [ condition2 ]; then
# Code to execute if condition2 is true
else
# Code to execute if all conditions are false
fi
case "$variable" in
pattern1)
# Code to execute for pattern1
;;
pattern2)
# Code to execute for pattern2
;;
*)
# Code to execute if no patterns match
;;
esac
Bash uses the test command (or [) to evaluate conditions. Here are some common tests:
-eq
: Equal-ne
: Not equal-lt
: Less than-le
: Less than or equal-gt
: Greater than-ge
: Greater than or equal-z
: Empty string-n
: Non-empty string!
: Logical NOT-f
: Check if file exists and is a regular file-d
: Check if directory exists
You can combine conditions using logical operators:
&&
: Logical AND||
: Logical OR