case statement bash code example

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

#!/bin/bash
 
read -p "Enter your choice [yes/no]:" choice
 
case $choice in
     yes)
          echo "Thank you"
          echo "Your type: Yes"
          ;;
     no)
          echo "Ooops"
          echo "You type: No"
          ;;
     *)
          echo "Sorry, invalid input"
          ;;
esac

Example 3: bash case statement

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

Example 4: bash case statement

bash case notation