Multiple Conditions in Shell

Level 3142 keystrokes

An if/elif chain tests several conditions; && inside [[ ]] requires both to be true. age is 25 and status is "active", so it prints "Eligible user".

Shell code

# Values to test
1age=25
2status="active"
# -ge is a numeric >=; && requires both tests to be true
3if [[ $age -ge 18 && $status == "active" ]]; then
# Runs here: 25 >= 18 and status is "active"
4 echo "Eligible user"
# elif is checked only if the first condition was false
5elif [[ $age -lt 18 ]]; then
6 echo "Too young"
# fi closes the if block
7fi

Output

Eligible user
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • "× 8
  • [× 4
  • ]× 4
  • =× 4
  • $× 3
  • -× 2
  • ;× 2
  • &× 2

More Shell examples at level 3

← Shell typing practice