Skip to main content

Conditionals

Conditionals control execution flow based on test results.

  • if-then-else:
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
  • test or [ ] evaluate expressions.
  • [[ ]] offers extended tests (pattern matching, regex).
  • case matches 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: