Bash Case Statement
Bash Case Statement

What is a bash case statement?

A bash case statement is a way to control the flow of a bash script. It checks the value of a variable and compares it with different patterns. When it finds a match, it runs the commands for that option. If no match is found, it runs a default action.

This is much easier than writing many if-else statements. Think of it like a menu: you choose one option, and the script knows what to do.

How a bash case statement works

The structure is simple:

  1. Start with the word case followed by the variable you want to test
  2. Add the word in
  3. Write possible patterns and commands for each match
  4. End each pattern with two semicolons ( ;; )
  5. Use a star (*) for the default option
  6. Close with esac (which is case spelled backward)

Example 1: Color menu

echo "Pick a color:"
echo "1 Blue"
echo "2 Red"
echo "3 Yellow"
echo "4 Green"
read choice

case $choice in
  1) echo "Blue is a primary color";;
  2) echo "Red is a primary color";;
  3) echo "Yellow is a primary color";;
  4) echo "Green is a secondary color";;
  *) echo "This color is not available";;
esac

Explanation line by line:

  • echo "Pick a color:" → prints a message asking the user to pick a color.
  • echo "1 Blue", echo "2 Red", etc. → show the available options.
  • read choice → waits for the user to type a number and stores it in the variable choice.
  • case $choice in → starts the case statement and checks the value of choice.
  • 1) echo "Blue is a primary color";; → if the value is 1, it prints this message.
  • 2), 3), 4) → handle other valid choices.
  • *) echo "This color is not available";; → the star is the default option. It runs if the user enters something other than 1–4.
  • esac → ends the case statement.

Example 2: Days in a month

read -p "Enter a month: " month

case $month in
  February) echo "28 or 29 days";;
  April|June|September|November) echo "30 days";;
  January|March|May|July|August|October|December) echo "31 days";;
  *) echo "Unknown month";;
esac

Explanation line by line:

  • read -p "Enter a month: " month → shows a prompt asking for a month and saves the input in the variable month.
  • case $month in → begins the case statement and checks the value of month.
  • February) echo "28 or 29 days";; → if the user typed “February”, it prints this message.
  • April|June|September|November) → the bar symbol | means “or”, so this line matches any of those months and prints “30 days”.
  • January|March|...|December) → matches the other months and prints “31 days”.
  • *) echo "Unknown month";; → handles anything else that does not match.
  • esac → closes the statement.

Example 3: File type check

for file in *; do
  case $file in
    *.txt) echo "Processing text file: $file";;
    *.csv) echo "Processing CSV file: $file";;
    *) echo "Ignoring unknown file: $file";;
  esac
done

Explanation line by line:

  • for file in *; do → this loop goes through every file in the current folder. Each file name is stored in the variable file.
  • case $file in → starts the case statement and checks the current file name.
  • *.txt) echo "Processing text file: $file";; → if the file name ends with .txt, it prints that it is processing a text file.
  • *.csv) echo "Processing CSV file: $file";; → if the file name ends with .csv, it prints that it is processing a CSV file.
  • *) echo "Ignoring unknown file: $file";; → the default action for all other file types.
  • esac → ends the case statement.
  • done → ends the loop.

Why use a bash case statement?

  • It makes scripts shorter and easier to read
  • It avoids long and confusing if-else chains
  • It stops after finding the first match
  • It lets you group multiple patterns together

Quick template for bash case statement

Here’s the general structure:

case value in
  option1) commands;;
  option2) commands;;
  *) default commands;;
esac

Explanation:

  • case value in → begins the case statement and checks the value.
  • option1) → pattern to match.
  • commands;; → runs if the pattern matches.
  • *) → the default option for anything else.
  • esac → ends the case statement.

Common uses of bash case statement

The bash case statement is often used to:

  • Create menus for users
  • Handle different user inputs
  • Run commands depending on file names or file extensions

Final thoughts

The bash case statement is one of the most useful tools in bash scripting. It gives your script a clear list of choices instead of many if-else lines.

By understanding the structure and seeing how it works in examples, you can start writing scripts that are simple, clear, and beginner-friendly. Whenever you have multiple options to check, case is the right tool to use.

LEAVE A REPLY

Please enter your comment!
Please enter your name here