AnyCompiler

Conditionals

if/then/else/fi forms a conditional, and [ ... ] — spaces around every token are required — is actually a call to the test command, not special syntax. -eq compares numbers; string comparisons use = instead.

x=5
if [ "$x" -eq 5 ]; then
  echo "x is five"
fi
Output
x is five

[[ ... ]] is Bash's own, more modern conditional syntax — it supports && and || directly inside the brackets, and doesn't require quoting variables as defensively as [ ] does.

x=5
if [[ $x -eq 5 && $x -lt 10 ]]; then
  echo "x is five and less than ten"
fi
Output
x is five and less than ten

Next: loops — for and while, iterating over a list or a range.

Practice exercise

Given score=85, print "pass" if score is 60 or greater using [ ], otherwise print "fail".