ChrisAppStatic v0.4

Bash case Statements

What it does: Matches patterns cleanly (e.g., commands or file extensions) without long chains of if/elif.

What is it? case is a cleaner alternative to long chains of if/elif when matching against patterns.

Example: Simple service controller

#!/bin/bash
action="$1"
case "$action" in
  start)   echo "Starting…";; 
  stop)    echo "Stopping…";; 
  restart) echo "Restarting…";; 
  *) echo "Usage: $0 {start|stop|restart}"; exit 1;;
esac

← Back to home