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

Install VirtualBox on Ubuntu 18.04 from the Oracle Repository

VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…

10 hours ago

Install PostgreSQL on Ubuntu 18.04 with Remote Access Setup

PostgreSQL (also called Postgres) is a free, open-source, object-relational database management system with a strong reputation…

10 hours ago

Install VMware on Ubuntu 18.04: Workstation Player Setup Guide

VMware Workstation Player is a mature, stable virtualization platform that lets you run multiple isolated operating…

10 hours ago

Set Up a UFW Firewall on Ubuntu 18.04: Allow, Deny, and Manage

A properly configured firewall is one of the most important layers of security for any internet-facing server. UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules that ships pre-installed on Ubuntu. Its defaults are sensible: block all incoming connections, allow all outgoing connections. No outside traffic reaches your server unless you explicitly open a port. This guide covers how to configure a UFW firewall on Ubuntu 18.04, from setting default policies and application profiles to writing allow and deny rules for specific ports, IPs, and subnets. <strong>Prerequisite:</strong>&nbsp;You&nbsp;need&nbsp;sudo&nbsp;access. Configure UFW Firewall on Ubuntu: Defaults, SSH, and Application Profiles If UFW is not installed, add it with: bashsudo…

10 hours ago

Disable UFW Firewall on Ubuntu 18.04: Stop, Reset, and Re-enable

UFW (Uncomplicated Firewall) is a user-friendly front-end for managing iptables rules on Ubuntu. It ships pre-installed…

11 hours ago

Install Apache Cassandra on Ubuntu 18.04: NoSQL Setup Guide

Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…

1 day ago