Free Republic
Browse · Search
General/Chat
Topics · Post Article

Skip to comments.

Bash Case Statement
Linuxize.com ^ | 28 February 2019 | Linuxize

Posted on 03/01/2019 10:15:34 AM PST by ShadowAce

Bash case statements are generally used to simplify complex conditionals when you have multiple different choices. Using the case statement instead of nested if statements will help you make your bash scripts more readable and easier to maintain.

The Bash case statement has a similar concept with the Javascript or C switch statement. The main difference is that unlike the C switch statement the Bash case statement doesn’t continue to search for a pattern match once it has found one and executed statements associated with that pattern.

In this tutorial, we will cover the basics of the Bash case statements and show you how to use them in your shell scripts.

Case Statement Syntax

The Bash case statement takes the following form:

case EXPRESSION in

PATTERN_1) STATEMENTS ;;

PATTERN_2) STATEMENTS ;;

PATTERN_N) STATEMENTS ;;

*) STATEMENTS ;;
esac

Case Statement Example

Here is an example using the case statement in a bash script that will print the official language of a given country:

languages.sh

#!/bin/bash

echo -n "Enter the name of a country: "
read COUNTRY

echo -n "The official language of $COUNTRY is "

case $COUNTRY in

Lithuania)
echo -n "Lithuanian"
;;

Romania | Moldova)
echo -n "Romanian"
;;

Italy | "San Marino" | Switzerland | "Vatican City")
echo -n "Italian"

*)
echo -n "unknown"
;;

esac

Save the custom script as a file and run it from the command line.

The script will ask you to enter a country. For example, if you type “Lithuania” it will match the first pattern and the echo command in that clause will be executed.

The script will print the following output:

Enter the name of a country: Lithuania
The official language of Lithuania is Lithuanian

If you enter a country that doesn’t match any other pattern except the default wildcard asterisk symbol, let’s say Argentina the script will execute echo command inside the default clause.

Enter the name of a country: Argentina
The official language of Argentina is unknown

Conclusion

By now you should have a good understanding of how to write bash case statements. They are often used to pass parameters to a shell script from the command line. For example, the init scripts are using case statements for starting, stopping or restarting services.


TOPICS: Computers/Internet
KEYWORDS: linux

1 posted on 03/01/2019 10:15:34 AM PST by ShadowAce
[ Post Reply | Private Reply | View Replies]

To: rdb3; Calvinist_Dark_Lord; JosephW; Only1choice____Freedom; Ernest_at_the_Beach; martin_fierro; ...

2 posted on 03/01/2019 10:15:55 AM PST by ShadowAce (Linux - The Ultimate Windows Service Pack)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

Lol - read that as “Bush case statement” and was wondering what W had said now :)


3 posted on 03/01/2019 10:18:27 AM PST by Skywise
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce
The case statement is extremely powerful, and can greatly simplify iterating over multiple options, as opposed to using long 'if ... then ... else' loops. Personally, I think it can make a script more legible than nested loops, which can easily get out of hand. Also, the final "*)" construct makes it easy to validate input. If your input doesn't fit the rather sharply defined criteria, you have an easy place to handle it.

ShadowAce, I'd recommend a post on 'while' loops next, as they are related IMO, one on functions would be cool too.

4 posted on 03/01/2019 11:52:04 AM PST by zeugma (Power without accountability is fertilizer for tyranny.)
[ Post Reply | Private Reply | To 1 | View Replies]

To: ShadowAce

You might like...

Linux servers targeted by new Chinese crypto-mining group
https://www.zdnet.com/article/linux-servers-targeted-by-new-chinese-crypto-mining-group/

Interesting stuff.
The ‘FREE MONEY’ mining will cause lots of trouble.

I’m just a lowly user, barely remember how to open terminal.
And all the mnemonics that make my brain hurt!!!

Long time user and I like it.


5 posted on 03/02/2019 9:27:12 AM PST by DUMBGRUNT ("The enemy has overrun us. We are blowing up everything. Vive la France!")
[ Post Reply | Private Reply | To 1 | View Replies]

Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.

Free Republic
Browse · Search
General/Chat
Topics · Post Article

FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson