Bourne/bash shell scripts: case statement

The case statement is an elegant replacement for if/then/else if/else statements when making numerous comparisons. This recipe describes the case statement syntax for the Bourne shells (sh, ksh, bash, zsh, etc.).


case "$var" in
value1)
commands;
;;
value2)
commands;
;;
*)
commands;
;;
esac

The case statement compares the value of the variable ($var in this case) to one or more values (value1, value2, …). Once a match is found, the associated commands are executed and the case statement is terminated. The optional last comparison *) is a default case and will match anything.

For example, branching on a command line parameter to the script, such as ‘start’ or ‘stop’ with a runtime control script. The following example uses the first command line parameter ($1):

case "$1" in
'start')
/usr/app/startup-script
;;
'stop')
/usr/app/shutdown-script
;;
'restart')
echo "Usage: $0 [start|stop]"
;;
esac

The Conversation

Follow the reactions below and share your own thoughts.

  • Anonymous

    case “$1” in
    stdin|
    -)
    commands
    ;;

    What does stdin|
    -) mean here?
    Can somebody explain it?

    • newbie

      stdin means “standard input” from the keyboard (file descriptor=0)
      stdout means “standard output” to the computer screen (file descriptor=1)
      stderr means “standard error”; output for error messages (file descriptor=2)

  • S.Raghu

    How to branch if the value of the variable is ‘*’ ?
    For eg parameters passed to a program are 2 numbers and an arithmetical operand.

    • Garry Hurley Jr

      Simple. You need to use the escape character on the switch, and you escape it as a parameter. Here is a basic example of that line

      #!/bin/bash
      case $1 in
      %)
      echo “%”
      ;;
      *)
      echo “*”
      ;;
      *)
      echo “none of the above”
      ;;
      esac

      Sample output:

      snoopy:~>./testBash *
      *

      My suggestion is to use the ‘x’ for multiplication instead if you don’t want your users to have to escape the ‘*’ every time they use the program.

  • Patrick

    Nice this one was really usefull, but the last one didn’t seem to work for me.
    It would whine about a syntax error, so I removes the ! in front and added a *) to catch all the rest, in the case I just did a echo “parameter is valid”

    But thanx a lot

  • Mark

    Thanks!

  • #!/bin/sh

    echo “Enter your choice:”
    read username

    case “$username” in
    ‘start’)
    echo ‘starting…………’;;
    ‘stop’)
    echo ‘stopping………..’;;
    ‘restart’)
    echo ‘restarting………’;;
    esac

    ##################
    Output:
    ./test.sh

    Enter your choice:
    start
    starting…………

  • Ocho

    read input
    case “${input}” in
    y|Y|yes|YES|Yes)
    echo “Do some yes things…”
    ;;
    n|N|no|NO|No)
    echo “Do some no things”
    ;;
    *)
    echo “${input} is not a valid input. ”
    # loop back in via a function or return 1 or quit etc.
    ;;
    esac

  • emma

    thanks