bash case examples

Example 1: bash case statement

case "$1" in
        start)
            start ;;
        stop)
            stop ;;
        restart)
            stop; start ;;
        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
esac

Example 2: bash case statement

case expression in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    ...
esac

Example 3: bash switch case

case $1 in
start)
    #Start logic
    ;;
stop)
    # Stop logic
    ;;
*)
	# Default logic
    echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
esac

Example 4: case statement example shell scripting

#!/bin/sh

echo "Please talk to me ..."
while :
do
  read INPUT_STRING
  case $INPUT_STRING in
	hello)
		echo "Hello yourself!"
		;;
	bye)
		echo "See you again!"
		break
		;;
	*)
		echo "Sorry, I don't understand"
		;;
  esac
done
echo 
echo "That's all folks!"

Example 5: bash case statement

bash case notation