Bash Case Statement
A bash case statement is a way to control the flow of a bash script. It checks the value of a variable and compares it with different patterns. When it finds a match, it runs the commands for that option. If no match is found, it runs a default action.
This is much easier than writing many if-else statements. Think of it like a menu: you choose one option, and the script knows what to do.
The structure is simple:
echo "Pick a color:"
echo "1 Blue"
echo "2 Red"
echo "3 Yellow"
echo "4 Green"
read choice
case $choice in
1) echo "Blue is a primary color";;
2) echo "Red is a primary color";;
3) echo "Yellow is a primary color";;
4) echo "Green is a secondary color";;
*) echo "This color is not available";;
esac
Explanation line by line:
echo "Pick a color:"
→ prints a message asking the user to pick a color.echo "1 Blue"
, echo "2 Red"
, etc. → show the available options.read choice
→ waits for the user to type a number and stores it in the variable choice
.case $choice in
→ starts the case statement and checks the value of choice
.1) echo "Blue is a primary color";;
→ if the value is 1
, it prints this message.2)
, 3)
, 4)
→ handle other valid choices.*) echo "This color is not available";;
→ the star is the default option. It runs if the user enters something other than 1–4.esac
→ ends the case statement.read -p "Enter a month: " month
case $month in
February) echo "28 or 29 days";;
April|June|September|November) echo "30 days";;
January|March|May|July|August|October|December) echo "31 days";;
*) echo "Unknown month";;
esac
Explanation line by line:
read -p "Enter a month: " month
→ shows a prompt asking for a month and saves the input in the variable month
.case $month in
→ begins the case statement and checks the value of month
.February) echo "28 or 29 days";;
→ if the user typed “February”, it prints this message.April|June|September|November)
→ the bar symbol |
means “or”, so this line matches any of those months and prints “30 days”.January|March|...|December)
→ matches the other months and prints “31 days”.*) echo "Unknown month";;
→ handles anything else that does not match.esac
→ closes the statement.for file in *; do
case $file in
*.txt) echo "Processing text file: $file";;
*.csv) echo "Processing CSV file: $file";;
*) echo "Ignoring unknown file: $file";;
esac
done
Explanation line by line:
for file in *; do
→ this loop goes through every file in the current folder. Each file name is stored in the variable file
.case $file in
→ starts the case statement and checks the current file name.*.txt) echo "Processing text file: $file";;
→ if the file name ends with .txt
, it prints that it is processing a text file.*.csv) echo "Processing CSV file: $file";;
→ if the file name ends with .csv
, it prints that it is processing a CSV file.*) echo "Ignoring unknown file: $file";;
→ the default action for all other file types.esac
→ ends the case statement.done
→ ends the loop.Here’s the general structure:
case value in
option1) commands;;
option2) commands;;
*) default commands;;
esac
Explanation:
case value in
→ begins the case statement and checks the value.option1)
→ pattern to match.commands;;
→ runs if the pattern matches.*)
→ the default option for anything else.esac
→ ends the case statement.The bash case statement is often used to:
The bash case statement is one of the most useful tools in bash scripting. It gives your script a clear list of choices instead of many if-else lines.
By understanding the structure and seeing how it works in examples, you can start writing scripts that are simple, clear, and beginner-friendly. Whenever you have multiple options to check, case is the right tool to use.
Introduction When it comes to cybersecurity, speed and privacy are critical. Public vulnerability databases like…
If you are working with Linux or writing bash scripts, one of the most common…
Why Do We Check Files in Bash? When writing a Bash script, you often work…
If you’re learning Bash scripting, one of the most useful features you’ll come across is…
If you are new to Bash scripting or Linux shell scripting, one of the most…
How Does a Firewall Work Step by Step? What Is a Firewall and How Does…