• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Bash Case Statement

Amit Jotwani

Amit Jotwani

Developer Relations, Warp

Published: 8/3/2023

About Terminus

A case statement is a conditional control structure that allows a selection to be made between several sets of program statements. It is a popular alternative to the if-then-else statement when you need to evaluate multiple different choices.

Case Statement Syntax

case expression in
    pattern1 )
      statements ;;
    pattern2 )
      statements ;;
    ...
  esac

To create a case statement:

  1. 1. Start with the word case, and follow it with a variable or an expression you’re trying to match, and end the line with the word in.
  2. 2. Next, list a pattern or value you want to match against the expression or variable in the case statement. End the pattern a with a parenthesis.
  3. 3. Finally, insert the commands you’d like to be executed if the pattern is matched. Be sure to add a double semicolon ;; when you’d like the execution to stop.

Now when the case statement is run, the commands following the first pattern match will be executed. The execution will stop when a double semicolon is reached, and the script will exit the case statement.

When to use If-else vs a Case statement

Much like the if-then-else statement, the bash case statement is a conditional statement used to vary the flow of your shell script.

While the if-else statement is used to choose between two options, the case statement is helpful when you have multiple different choices. Case statements are a lot easier to read and maintain compared to nested if-then-else statements you may otherwise use.

In fact, a good rule of thumb is if you find yourself using an if statement to compare the same variable or expression against different values or patterns, it’s probably a good idea to use a case statement instead.

Here’s an example of a somewhat complex if-else statement with multiple nested if-else statements.

if-else statement with nested if-else statements

This can be simplified with a case statement. As you can see, this is a lot more readable than the nested if-else statements.

A case statement that is much more readable

One thing to note is that the case statement stops searching for a pattern match as soon as it finds one. It's not a loop, as in it does not execute a block of code for n times. It stops at the first instance of the pattern match it's looking for.

💡Tip from Warp

Also, it’s a good practice to use the wildcard asterisk symbol (*) as a final pattern to define the default case. This pattern will always match, and it tells the interpreter that if no other pattern matches, default to this match.

Using a wildcard (*) as a final pattern

🧠 Did you know?

If you’re curious about the peculiar syntax of the case statement in bash, you’re not alone! Here’s this Stack Overflow question answering it.

TL;DR:

The Bash syntax came from Bourne (of Bourne shell fame). He had worked on Algol, and liked it enough to model some of the shell syntax on Algol. Algol uses reversed keywords to mark the ends of constructs, so 'case ... esac' was appropriate. The reason that loops do not end with 'od' is that there was already a command 'od' in Unix - octal dump. So, 'done' is used instead.

Written by

Amit Jotwani

Amit Jotwani

Developer Relations, Warp

Filed Under

Related Articles

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash
Gabriel Manricks

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Bash

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Neeran Gul

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Razvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Razvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Razvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Neeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Brett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Razvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Zev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Amit Jotwani

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac