How To

Bash Case Statement: How To Match Patterns In Shell Scripts

Introduction

The Bash case statement is used to match one value against multiple patterns. It is very useful when you want to create menu-based scripts, check user input, handle different options, or run different commands based on a selected value.

If you are learning Bash scripting, the case statement is an important topic after understanding if else. While if else is good for simple conditions, the case statement is cleaner when you have many possible choices.

Bash case statements are commonly used in Linux automation, cybersecurity scripts, system administration tools, backup scripts, and command-line menus.

What Is A Case Statement In Bash?

A case statement compares a value with different patterns. When a matching pattern is found, the related command is executed.

Basic syntax:

case value in    pattern1)        command        ;;    pattern2)        command        ;;    *)        default command        ;;esac

The case statement starts with case and ends with esac. The ;; symbol is used to end each pattern block.

Basic Bash Case Statement Example

Create a new Bash script:

nano case-example.sh

Add the following code:

#!/bin/bashday="Monday"case $day in    Monday)        echo "Today is Monday"        ;;    Tuesday)        echo "Today is Tuesday"        ;;    Wednesday)        echo "Today is Wednesday"        ;;    *)        echo "Unknown day"        ;;esac

Save the file and run it:

chmod +x case-example.sh./case-example.sh

Output:

Today is Monday

Bash Case Statement With User Input

The case statement is often used with user input.

#!/bin/bashecho "Choose an option:"echo "1. Show current user"echo "2. Show current directory"echo "3. Show current date"read -p "Enter your choice: " choicecase $choice in    1)        whoami        ;;    2)        pwd        ;;    3)        date        ;;    *)        echo "Invalid option"        ;;esac

This script creates a simple command-line menu. Based on the user’s choice, it runs a different Linux command.

Using Multiple Patterns In Bash Case

You can match multiple patterns using the | symbol.

#!/bin/bashread -p "Enter yes or no: " answercase $answer in    yes|Yes|y|Y)        echo "You selected yes"        ;;    no|No|n|N)        echo "You selected no"        ;;    *)        echo "Invalid answer"        ;;esac

This is useful when you want to accept different versions of the same input.

Conclusion

The Bash case statement is a clean and simple way to match patterns in shell scripts. It is especially useful when handling multiple choices, user input, and menu-based scripts.

For beginners, learning the case statement helps you write better Bash scripts with clear logic. It is widely used in Linux automation, cybersecurity tools, server scripts, and command-line utilities.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

5 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

5 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

5 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

5 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

5 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

6 days ago