New Course: Agentic AI with Python
LinuxBash Shell Scripting

Case Statement in Shell Scripting

Replace long if-elif chains with the Bash case statement, match a value against several patterns, and handle everything else with a default branch.

In Conditionals in Shell Scripting you learned to make a script choose between paths. if and else are perfect for a yes or no question. But many decisions are not yes or no. They are "which one of these": start, stop or restart. Dev, test or prod. With if you would write a chain like this:

if [ "$option" = "start" ]; then
    echo "Starting server"
elif [ "$option" = "stop" ]; then
    echo "Stopping the server"
elif [ "$option" = "restart" ]; then
    echo "Restarting the server"
else
    echo "Invalid option"
fi

It works, but the same variable is compared again and again, and the reader has to go through every line to see the choices.

The case statement is made for exactly this. You name the value once and list the choices under it. If you know the switch statement from Java, this is the Bash version of it.

The syntax

case $variable in
    pattern1)
        commands
        ;;
    pattern2)
        commands
        ;;
    *)
        default commands
        ;;
esac
PartMeaning
case $variable inThe value you want to check
pattern)One possible value. The ) ends the pattern
;;End of this branch. Bash leaves the case statement here
*)Matches anything. This is the default branch, used when nothing above matched
esacEnd of the case statement. It is case written backwards, like fi for if

Bash checks the patterns from top to bottom and runs the commands of the first one that matches. The rest are skipped.

Bash_Case_Statement

Step 1: A server control script

vi case.sh
case.sh
#!/bin/bash

echo "Enter the option:"

read option

case $option in

start)
        echo "Starting server"
        ;;
stop)
        echo "Stopping the server"
        ;;
restart)
        echo "Restarting the server"
        ;;
*)
        echo "Invalid option"
        ;;

esac

The script only prints messages. In a real script, each branch would hold the actual command, for example sudo systemctl start jenkins in the start branch.

chmod u+x case.sh
./case.sh
Enter the option:
start
Starting server

Run it again with other inputs:

Enter the option:
stop
Stopping the server
Enter the option:
juststop
Invalid option

juststop matches none of the three patterns, so the *) branch runs.

The mistake from the lecture

The first run in the lecture did not go as planned:

Enter the option:
start
Invalid option

The line in the script was:

case option in

The $ was missing. Without it, Bash does not read the variable. It compares the plain word option against start, stop and restart, and that word never matches, so every input ends up in the default branch. With case $option in the script works.

When a case statement always goes to *), check this line first.

A second slip was in a pattern, typed as retstart). Bash does not complain about that either, because a pattern can be any text. The script simply would never react to restart. Read your patterns carefully, the shell will not catch a spelling mistake for you.

Step 2: Choosing a deployment environment

The same structure, with a more realistic use. The script asks where to deploy:

case.sh
#!/bin/bash

echo "Enter the environment for deployment:"

read env

case $env in

dev)
        echo "Deploying to DEV"
        ;;
test)
        echo "Deploying to TEST"
        ;;
prod)
        echo "Deploying to PROD"
        ;;
*)
        echo "Unknown environment"
        ;;

esac
./case.sh
Enter the environment for deployment:
prod
Deploying to PROD
Enter the environment for deployment:
dev
Deploying to DEV

Deployment scripts in real projects look very much like this. One script, one argument, and the case statement picks the right servers, the right config file and the right safety checks for that environment.

Patterns can do more

The lecture uses exact words as patterns. A pattern can also cover several values or a whole group of them.

Several values for one branch. Separate them with |, which means "or":

case $option in
    start|START|Start)
        echo "Starting server"
        ;;
    stop|STOP|Stop)
        echo "Stopping the server"
        ;;
esac

Patterns are case sensitive. Without this, a user who types START lands in the default branch.

Wildcards. The same symbols you use with file names work here:

PatternMatches
y*Anything that starts with y: y, yes, yeah
[0-9]A single digit
[yY]y or Y
*.logAnything that ends in .log
*Everything

A small confirm prompt that accepts different ways of saying yes:

confirm.sh
#!/bin/bash

read -p "Delete old backups? (yes/no): " answer

case "$answer" in
    [yY]|[yY][eE][sS])
        echo "Deleting..."
        ;;
    [nN]|[nN][oO])
        echo "Cancelled."
        ;;
    *)
        echo "Please answer yes or no."
        exit 1
        ;;
esac

Keep the default branch last

Bash stops at the first pattern that matches. *) matches everything, so if you put it first, no other branch will ever run. It always goes at the bottom.

case with command line arguments

Typing the answer at a prompt is fine for a demo. In automation, the choice usually comes as a command line argument, so that nobody has to be there to type it:

deploy.sh
#!/bin/bash

case "$1" in
    dev)
        echo "Deploying to DEV"
        ;;
    test)
        echo "Deploying to TEST"
        ;;
    prod)
        echo "Deploying to PROD"
        ;;
    *)
        echo "Usage: $0 {dev|test|prod}"
        exit 1
        ;;
esac
./deploy.sh prod
Deploying to PROD
./deploy.sh
Usage: ./deploy.sh {dev|test|prod}

Many service scripts on Linux follow this exact form. The default branch doubles as the help message.

if-else or case

Use if whenUse case when
The question is yes or noOne value is compared against a list of choices
You compare numbers: greater, less, betweenYou match words or patterns
You test files: exists, is a directory, is executableYou build a menu or handle options like start, stop, restart
The conditions involve different variablesEvery branch looks at the same variable

Try It Yourself

  1. Create the first case.sh and try start, stop, restart and a wrong word.
  2. Remove the $ from case $option in, run it with start, and see the default branch take over. Put the $ back.
  3. Change the script to the dev, test and prod version. Make prod and production both go to the PROD branch using |.
  4. Create deploy.sh that takes the environment from $1. Run it with no argument and check the exit status with echo $?.
  5. Write filetype.sh that takes a file name as $1 and prints Shell script for *.sh, Text file for *.txt, Log file for *.log and Unknown type for anything else.

Official references


Written By: Shramik Masti

How is this guide?

Last updated on