An ad-free and cookie-free website.
Learn to build a program menu within a script at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux bash Shell Script case Statement with Examples (3:55)
Our ninety-seventh word, or command (keyword) to memorize is
case from our category
case allows you to build a
|None for the case keyword|
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).
case command (keyword) has
no traditional options and no arguments as it has its
own specific structure.
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
So why is
case an important command (keyword)?
case offers a visually appealing
and easy to code alternative to
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
case in a script here,
because that's the best way to go. These usually go multi-lines, right?
(Below is the screen from within
Let's walk through the menu setup here, first with a screen
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
# case code section starts
case keyword, a variable,
in and then the search term,
or patterns before the parentheses and then the commands go between
;; double semi-colons.
For the last one, we use a star
catch-all and it ends in
case spelled backwards.
Ctrl-x to leave
y to confirm saving.)
And last, let's give this a go and run
(The first line of the bash shell script clears the screen.)
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.
Okay now you know how to use
And you know the syntax for commands, options and arguments.
One last tip about the
(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).
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
This is not a website to lose contact with. Subscribe at YouTube, follow @factorpad on Twitter and join the no-spam email list.
whilestatement, hit Back.
forstatement, click Next.
A newly-updated free resource. Connect and refer a friend today.