Conditionals
Conditionals control execution flow based on test results.
if-then-else:
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
testor[ ]evaluate expressions.[[ ]]offers extended tests (pattern matching, regex).casematches patterns:
case $option in
start) echo "Starting";;
stop) echo "Stopping";;
*) echo "Unknown";;
esac
Input:
age=20
if [[ $age -ge 18 ]]; then
echo "Adult"
else
echo "Minor"
fi
Output:
Adult
Explanation: [[ $age -ge 18 ]] checks numeric comparison.
References: