thelinuxvault blog

Mastering the `case` Command in Linux: An In - Depth Guide

In the world of Linux shell scripting, making decisions based on the value of a variable is a common task. One efficient way to handle multiple conditional checks is by using the case command. The case command provides a more concise and readable alternative to a series of nested if - else statements when you need to test a variable against multiple possible values. In this blog, we will explore the case command in Linux in detail, including its syntax, usage with various examples, common practices, and best practices.

2026-06

Table of Contents#

  1. Syntax of the case Command
  2. Simple Example of case Command
  3. Using Wildcards in case Statements
  4. Multiple Patterns in case
  5. Common Practices
  6. Best Practices
  7. Conclusion
  8. References

Syntax of the case Command#

The basic syntax of the case command in Linux is as follows:

case expression in
    pattern1)
        statements1
        ;;
    pattern2)
        statements2
        ;;
    pattern3)
        statements3
        ;;
    *)
        default_statements
        ;;
esac
  • expression: This is the variable or value that you want to test.
  • pattern1, pattern2, etc.: These are the possible values or patterns against which the expression is compared.
  • statements1, statements2, etc.: These are the commands or actions that will be executed if the corresponding pattern matches the expression.
  • ;;: This is used to terminate each pattern - statement block.
  • *): This is a wildcard pattern that acts as a default case. It will match any value that does not match the previous patterns.
  • esac: This is the end marker for the case statement, which is just case spelled backwards.

Simple Example of case Command#

Let's consider a simple example where we have a variable day and we want to print a message based on its value.

#!/bin/bash
day="Monday"
 
case $day in
    Monday)
        echo "It's the start of the workweek."
        ;;
    Friday)
        echo "Yay! It's almost the weekend."
        ;;
    Saturday|Sunday)
        echo "It's the weekend, time to relax."
        ;;
    *)
        echo "It's a regular weekday."
        ;;
esac

In this example, since the value of day is "Monday", the output will be:

It's the start of the workweek.

Using Wildcards in case Statements#

Wildcards can be used in case patterns to match a set of values. The most commonly used wildcards are * (matches any number of any characters) and ? (matches any single character).

#!/bin/bash
file="document.txt"
 
case $file in
    *.txt)
        echo "This is a text file."
        ;;
    *.pdf)
        echo "This is a PDF file."
        ;;
    *)
        echo "Unknown file type."
        ;;
esac

In this example, the *.txt pattern matches any file name that ends with .txt, so the output will be:

This is a text file.

Multiple Patterns in case#

You can specify multiple patterns for a single block of statements by separating them with a vertical bar |.

#!/bin/bash
fruit="apple"
 
case $fruit in
    apple|banana|cherry)
        echo "This is a common fruit."
        ;;
    kiwi|mango)
        echo "This is an exotic fruit."
        ;;
    *)
        echo "I don't know this fruit."
        ;;
esac

In this example, since the value of fruit is "apple", the output will be:

This is a common fruit.

Common Practices#

  • Error Handling: Always include a default case (*) in your case statements. This ensures that if the variable does not match any of the specified patterns, there is a fallback action to handle the situation.
  • Use Clear Patterns: Choose patterns that are easy to understand and maintain. Avoid using overly complex regular expressions or patterns that are difficult to decipher.
  • Indentation: Proper indentation is crucial for readability. Indent the statements inside each pattern block to clearly distinguish them from the pattern itself.

Best Practices#

  • Keep It Short: If you find yourself with an extremely long case statement, consider refactoring your code. You might be able to break the logic into smaller functions or use an array and a loop to handle the cases more efficiently.
  • Test Thoroughly: Before deploying your shell script with case statements, test all possible cases to ensure that the correct actions are taken for each input.

Conclusion#

The case command in Linux is a powerful and flexible tool for handling multiple conditional checks in shell scripts. It offers a more readable and concise alternative to nested if - else statements, especially when dealing with multiple possible values of a variable. By following the common and best practices outlined in this blog, you can write more robust and maintainable shell scripts.

References#

This blog provides a comprehensive overview of the case command in Linux. With the examples and practices shared, you should now be well - equipped to use the case command effectively in your shell scripting endeavors.