Case Statement in Shell

Level 3110 keystrokes

case statement matches a value against multiple patterns.

Shell code

# Compares the first script argument with each pattern in order
1case "$1" in
# start) matches the word start; ;; ends the branch
# The output shown assumes the script was run with start
2 start) echo "Starting..." ;;
3 stop) echo "Stopping..." ;;
# * matches anything else, so it is the default; $0 is the script name
4 *) echo "Usage: $0 {start|stop}" ;;
# esac (case spelled backwards) closes the statement
5esac

Output

Starting...
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
  • $× 2
  • :× 1
  • {× 1
  • }× 1
  • *× 1
  • |× 1

More Shell examples at level 3

← Shell typing practice