Bash Scripting

Mastering the Bash Case Statement with Simple Examples

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.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

23 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

24 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

1 day ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

1 day ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

1 day ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

1 day ago