Linux Tactic

Mastering Bash Case Command: Examples and Syntax

Bash Case Command: The Versatile Tool for Switch-Case Statements

Have you ever come across a situation where you needed to compare a variable with multiple values and execute different actions, depending on its value? If yes, then you would understand how cumbersome and tedious the process can be.

Manually checking every value and executing the desired actions can be time-consuming and prone to errors. This is where the Bash Case Command comes in handy.

The Bash Case Command is a versatile tool that enables you to test a variable against a list of possible values and perform actions based on the matching value. In this article, we will explore the basics of the Bash Case Command and provide you with some examples to get started.

Basic Syntax and Functionality

The Bash Case Command is a construct used to implement switch-case statements in Bash. It enables you to execute different sections of code based on the value of a variable.

The basic syntax of the Bash Case Command is as follows:

“`bash

case variable in

pattern1)

command1;;

pattern2)

command2;;

pattern3)

command3;;

*)

default_command;;

esac

“`

The Bash Case Command starts with the `case` keyword followed by the variable you want to test. The `in` keyword signi

fies the start of the patterns to match against.

Each pattern is enclosed in parentheses and ends with a double semicolon `;;`. The commands to execute for each pattern are written immediately after the matching pattern.

If none of the patterns match, then the `*` symbol is used to indicate the default action to execute. Example 1: Single Conditional Expression

Let’s take a look at a simple example of a Bash Case Command with only a single conditional expression:

“`bash

#!/bin/bash

fruit=”apples”

case $fruit in

“apples”)

echo “I like apples.”;;

“oranges”)

echo “I like oranges.”;;

*)

echo “I don’t like fruit.”;;

esac

“`

In this example, the variable `$fruit` is tested against the patterns `”apples”` and `”oranges”`. Since the value of `$fruit` is `”apples”`, the script output will be `I like apples.`.

If the value of `$fruit` had been `”oranges”`, then the output would have been `I like oranges.`. Example 2: Multiple Case Statements with Pipe

The Bash Case Command can also handle multiple case statements separated by the pipe `|` symbol.

Consider the following example:

“`bash

#!/bin/bash

fruit=”bananas”

case $fruit in

“apples” | “pears”)

echo “I like apples and pears.”;;

“oranges” | “grapefruits”)

echo “I like oranges and grapefruits.”;;

*)

echo “I don’t like fruit.”;;

esac

“`

In this case, the variable `$fruit` is tested against the patterns `”apples”` and `”pears”` on one side of the pipe, and `”oranges”` and `”grapefruits”` on the other side. The output of the script will default to `I don’t like fruit.` since `$fruit` is `”bananas”` and does not match any of the patterns.

Example 3: Bash Case Command in an In

finite Loop

Another way to use the Bash Case Command is to execute it within an in

finite loop. Consider the following example that waits for the user to enter a command and executes it accordingly:

“`bash

#!/bin/bash

while true

do

read -p “Enter a command: ” cmd

case $cmd in

“exit”)

echo “Goodbye!”

exit;;

“ls”)

ls -l;;

“pwd”)

pwd;;

*)

echo “Unknown command.”;;

esac

done

“`

In this example, the script loops inde

finitely until the user enters the `exit` command. The other possible commands are `ls` that lists all

files in the current directory, and `pwd` that prints the current working directory.

If the user enters an invalid command, then the script outputs `Unknown command.`

Example 4: Command Line Argument Variable in Bash Case Statement

The Bash Case Command can also work with command line arguments. Consider the following example:

“`bash

#!/bin/bash

case $1 in

“start”)

echo “Starting up…”;;

“stop”)

echo “Shutting down…”;;

*)

echo “Unknown command.”;;

esac

“`

In this example, the script accepts a command line argument `$1` and executes the appropriate action based on the value of `$1`. If `$1` is `”start”`, then the output will be `Starting up…`.

If `$1` is `”stop”`, then the output will be `Shutting down…`. If `$1` is anything

else, then the output will be `Unknown command.`.

Example 5: Matching Month Names with Case Statement

The Bash Case Command can also match regular expressions, allowing you to use it to match partial strings. Consider the following example:

“`bash

#!/bin/bash

month=”June”

case $month in

“January” | “February” | “March”)

echo “It’s winter season.”;;

“April” | “May” | “June”)

echo “It’s spring season.”;;

“July” | “August” | “September”)

echo “It’s summer season.”;;

“October” | “November” | “December”)

echo “It’s fall season.”;;

*)

echo “Invalid month.”;;

esac

“`

In this example, the script checks the value of `$month`, which is `”June”`. Since the pattern for `”June”` is `”April” | “May” | “June”`, the output will be `It’s spring season.`.

The `|` symbol is used to match any of the patterns on either side. Example 6: Checking Variable in Bash Case Statement

The Bash Case Command can also be used to check for the existence of a variable.

Consider the following example:

“`bash

#!/bin/bash

if [ -n “$1” ]; then

case $1 in

“start”)

echo “Starting up…”;;

“stop”)

echo “Shutting down…”;;

*)

echo “Unknown command.”;;

esac

else

echo “No argument provided.”

fi

“`

In this example, the script checks if a command line argument has been provided. If so, it executes the Bash Case Command as in Example 4.

If no argument has been provided, the script outputs `No argument provided.`.

Summary of the Bash Case Command Examples

In conclusion, the Bash Case Command is a versatile tool that enables you to match a variable against multiple patterns and execute different actions depending on the match. We have explored several examples of this command, starting with the basic syntax and functionality, followed by examples with single and multiple patterns.

We have also seen how the Bash Case Command can be executed within an in

finite loop, handle command line arguments, match regular expressions, and check for the existence of a variable. By using the Bash Case Command, you can write more ef

ficient, maintainable, and error-free Bash scripts.

In conclusion, the Bash Case Command is an essential tool for matching variables against multiple patterns and executing different actions based on the match. By using the command, you can write more ef

ficient and error-free Bash scripts.

The article provides the basic syntax and functionality of the command and covers six examples ranging from single conditional expressions to command-line argument variables. Whether it is for checking variable existence or matching month names, the Bash Case Command is a versatile tool that can make your scripts more maintainable.

Try using the command in your scripts to make them more ef

ficient and save time.

Popular Posts