Skip to content

14. Bash Case Statement

Overview

The Bash case statement is a conditional control structure used when a script needs to choose between multiple possible conditions.

It is especially useful when there are several possible values to check. Compared with using many if, elif, and else statements, case can make the script easier to read and maintain.

Basic Syntax

case expression in
    pattern1)
        commands
        ;;
    pattern2)
        commands
        ;;
    pattern3)
        commands
        ;;
    *)
        default commands
        ;;
esac

The case statement compares an expression against multiple patterns. When a matching pattern is found, its associated commands are executed.

Example

#!/bin/bash

echo "What is your preferred programming / scripting language"
echo "1) bash"
echo "2) perl"
echo "3) python"
echo "4) c++"
echo "5) I do not know!"

read choice

case $choice in
    1)
        echo "You selected bash"
        ;;
    2)
        echo "You selected perl"
        ;;
    3)
        echo "You selected python"
        ;;
    4)
        echo "You selected c++"
        ;;
    5)
        exit
        ;;
esac

Output

$ ./case.sh
What is your preferred programming / scripting language
1) bash
2) perl
3) python
4) c++
5) I do not know!
3
You selected python

Understanding the Script

Reading User Input

read choice

The read command waits for the user to enter a value and stores it in the choice variable.

For example, if the user enters:

3

then:

choice=3

case

case $choice in

The case statement evaluates the value stored in $choice.

If the value is 3, Bash looks for a matching pattern:

3)
    echo "You selected python"
    ;;

The corresponding command is executed.

Patterns

Each option is represented by a pattern:

1)
2)
3)
4)
5)

For example:

3)
    echo "You selected python"
    ;;

means:

If $choice matches 3, execute the command inside this block.

;;

;;

The ;; terminates the current case branch.

For example:

1)
    echo "You selected bash"
    ;;

After executing the command, Bash stops processing the current case statement.

esac

esac

esac marks the end of the case statement.

It is essentially case written backwards.

Default Case

A * pattern can be used as a default option when none of the other patterns match.

case $choice in
    1)
        echo "You selected bash"
        ;;
    2)
        echo "You selected perl"
        ;;
    *)
        echo "Invalid selection"
        ;;
esac

If the user enters:

9

the output will be:

Invalid selection

This is useful for handling unexpected input.

Using Multiple Patterns

Multiple patterns can be handled by the same block using |.

#!/bin/bash

read -p "Enter a letter: " letter

case $letter in
    a|A)
        echo "You entered A"
        ;;
    b|B)
        echo "You entered B"
        ;;
    *)
        echo "Unknown letter"
        ;;
esac

Here:

a|A)

matches either lowercase a or uppercase A.

Using Wildcards

case supports pattern matching, which makes it useful for working with strings.

#!/bin/bash

read -p "Enter a filename: " file

case $file in
    *.txt)
        echo "This is a text file"
        ;;
    *.sh)
        echo "This is a Bash script"
        ;;
    *.jpg|*.png)
        echo "This is an image file"
        ;;
    *)
        echo "Unknown file type"
        ;;
esac

Example:

Enter a filename: script.sh
This is a Bash script

Another example:

Enter a filename: photo.jpg
This is an image file

case vs if

A simple if statement might look like:

if [ "$choice" = "1" ]; then
    echo "You selected bash"
elif [ "$choice" = "2" ]; then
    echo "You selected perl"
elif [ "$choice" = "3" ]; then
    echo "You selected python"
else
    echo "Invalid selection"
fi

The same logic can be expressed more cleanly using case:

case $choice in
    1)
        echo "You selected bash"
        ;;
    2)
        echo "You selected perl"
        ;;
    3)
        echo "You selected python"
        ;;
    *)
        echo "Invalid selection"
        ;;
esac

if is generally convenient for conditions involving comparisons and logical expressions, while case is particularly useful when matching one value against multiple possible patterns.

Practical Example

A case statement can be used to create a simple system administration menu:

#!/bin/bash

echo "Select an action:"
echo "1) Show current directory"
echo "2) Show logged-in user"
echo "3) Show system uptime"
echo "4) Exit"

read choice

case $choice in
    1)
        pwd
        ;;
    2)
        whoami
        ;;
    3)
        uptime
        ;;
    4)
        echo "Exiting..."
        exit 0
        ;;
    *)
        echo "Invalid option"
        ;;
esac

Example:

Select an action:
1) Show current directory
2) Show logged-in user
3) Show system uptime
4) Exit
2
root

Key Points

  • case is useful for handling multiple possible conditions.

  • The value being evaluated is written after case.

  • Each option consists of a pattern followed by ).

  • ;; terminates a case branch.

  • * can be used as a default or catch-all pattern.

  • Multiple patterns can be combined using |.

  • Wildcards such as * can be used for pattern matching.

  • esac marks the end of the case statement.

  • case is often cleaner than a long chain of if/elif statements when matching one value against many alternatives.