Case Statement in Shell

Level 3141 keystrokes

case compares the value of action with each pattern in order and runs the first match. Here action is "start", so it prints "Starting service...".

Shell code

# The value that case will test
1action="start"
# Compare $action with each pattern from top to bottom
2case $action in
# Matches "start"; ;; ends this branch
3 start) echo "Starting service..." ;;
# Matches "stop"
4 stop) echo "Stopping service..." ;;
# * matches anything else (default branch)
5 *) echo "Unknown action" ;;
# esac (case spelled backwards) closes the case
6esac

Output

Starting service...
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
  • ;× 6
  • .× 6
  • )× 3
  • *× 1
  • =× 1
  • $× 1

More Shell examples at level 3

← Shell typing practice