Faster Learning Tutorials

Linux bash shell script case statement with examples

case allows you to build a program menu within a script. (case is technically a bash keyword, not a command)
  1. Purpose - Learn what case is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with case.
  4. Script - Add case to our script and run it.
  5. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 22, 2016
Updated: August 6, 2018
In this tutorial, 97 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / case command

The Linux case command (keyword)


Learn to build a program menu within a script at the Linux command line.

Video Tutorial

Linux bash shell script case statement with examples (3:55)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our ninety-seventh word, or command (keyword) to memorize is case from our category Workflow.

case allows you to build a program menu.

Recall from videos (tutorials) #87 to #96, we've used a script to demonstrate workflow. Now we'll cover case and take a break from that script and return to it in video (tutorial) #100.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The case command (keyword) has no traditional options and no arguments as it has its own specific structure. case and the other option we covered for setting up menus if are both technically keywords.

Unlike most commands, help is not available with double-dash --help, here as case is a shell built-in covered in the bash manual page.

So why is case an important command (keyword)? Well, case offers a visually appealing and easy to code alternative to if, when setting up menus. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. We'll use nano to see the structure of case in a script here, because that's the best way to go. These usually go multi-lines, right?

$ nano bin/testcase

(Below is the screen from within nano.)

GNU nano 2.2.6 File: bin/testcase # From video 97, set up the structure for a menu using case, called testcase # menu setup clear echo " 1. Method 1 2. Method 2 3. Exit " read -p "Your choice (1 to 3): " choice # case code case $choice in 1) echo "You chose Method 1" exit ;; 2) echo "You chose Method 2" exit ;; 3) echo "Goodbye!" exit ;; *) echo "Invalid selection" exit ;; esac [ Read 29 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

Let's walk through the menu setup here, first with a screen clear, then echo a multi-line block and then a read statement to take data in, and that was from video (tutorial) #89, with a text block and the variable called choice.

Then the # case code section starts with the case keyword, a variable, the word in and then the search term, or patterns before the parentheses and then the commands go between there and ;; double semi-colons.

For the last one, we use a star * catch-all and it ends in esac, which is case spelled backwards.

(Hit Ctrl-x to leave nano and y to confirm saving.)

And last, let's give this a go and run testcase.

$ testcase

(The first line of the bash shell script clears the screen.)

1. Method 1 2. Method 2 3. Exit Your choice (1 to 3): 3 $ _

Here's our simple menu is sitting within this section, and we didn't wrap this in a loop, so all of this will end, and so let's try 3. Very good.

A Final Tip

Okay now you know how to use case. And you know the syntax for commands, options and arguments.

One last tip about the case command (keyword). So earlier when I said patterns, bash will accept expansions, classes or fixed letters if you'd like to put those in there.

Okay, thanks for visiting today. I hope this was a helpful introduction to the case command (keyword).

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

We love feedback. Reach us on our YouTube Channel. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the while statement, hit Back.
  • To cover another two looping commands before closing out the Series with the for statement, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / case command

linux case
bash case
shell case
case bash
shell script example
bash script example
script case
shell scripting tutorial
bash programming
shell script case
case esac
case menu
case statement
bash case statement
linux case statement
case keyword
linux script example
bash scripting
shell case statement
linux case tutorial