Bash Script: Case statement examples

If you already have some experience with writing Bash scripts, then you have probably needed to use conditional statements in the past. You may already be familiar with using if statements in a Bash script. Case statements work similarly but are more scalable and can handle many possibilities with ease.

Using case is much easier than writing many if and elif statements nested together, so if you have many conditions to test for, case is the obvious better option. In this tutorial, we will show you various examples of using a case statement in Bash on a Linux system.

In this tutorial you will learn:

  • How to structure a case statement in a Bash script
  • Real examples of case statements to try on your own system
Using a case statement inside of a Bash script on Linux
Using a case statement inside of a Bash script on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Structure of a case statement




First, let’s go over how a case statement is structured in a Bash script. This will familiarize you with the syntax so you can easily interpret the coming examples, and eventually write your own from scratch.

#!/bin/bash

case EXPRESSION in 

  pattern-1)
    statement 
    ;;

  pattern-2) 
    statement
    ;;

  pattern-3 | pattern-4) 
    statement 
    ;;

  *)
    statement 
    ;; 
esac

Here is what you need to know about the above case statement syntax.

  • A case statement begins with case and ends with esac.
  • The first pattern that matches a condition will be executed. Once one pattern is matched, the rest of the statements will be ignored.
  • Each pattern is terminated by a double semi colon ;;.
  • You can have a statement that gets triggered on multiple patterns, such as the case with pattern-3 and pattern-4 above.
  • You can have a wildcard that will trigger if no other patterns are matched. This is shown with an asterisk * for the final statement.
  • You can have as many pattern clauses as you want, as there is no limit.

Bash Script: Case statement examples

We will look at a few different scenarios below to see how to use case statements within a Bash script. Check all of the examples below to learn how.

  1. Let’s start with an example in which our case statement includes three possible patterns to match, which includes a wildcard.
    #!/bin/bash
    
    echo "what is your favorite operating system?"
    read os
    
    case $os in 
    
      linux)
        echo "you love Linux? we do too!"
        ;;
    
      bsd) 
        echo "BSD is a good system, too"
        ;;
    
      *)
        echo "you should consider an open source system"
        ;; 
    esac

    And here is what happens when we execute the script:

    $ ./test.sh 
    what is your favorite operating system?
    linux
    you love Linux? we do too!
    
    $ ./test.sh 
    what is your favorite operating system?
    bsd
    BSD is a good system, too
    
    $ ./test.sh 
    what is your favorite operating system?
    windows
    you should consider an open source system
    




    What is happening in the script? We are prompted to answer what our favorite operating system is. If we enter “linux,” pattern number 1 will be matched. If we enter “bsd,” pattern number 2 is matched. If neither of those conditions are met, then the wildcard will be triggered.

  2. Let’s look at a simple example which has statements tied to multiple patterns. This script will let us know whether today is a weekday or weekend.
    #!/bin/bash
    
    day=$(date +"%a")
    
    case $day in 
    
      Mon | Tue | Wed | Thu | Fri)
        echo "today is a weekday"
        ;;
    
      Sat | Sun) 
        echo "today is the weekend"
        ;;
    
      *)
        echo "date not recognized"
        ;; 
    esac

    And here is what happens when we execute the script:

    $ ./test.sh 
    today is a weekday
    

    What is happening in the script? The date +"%a" command is getting information about what day of the week it is. Then our case statement will check whether the result is Mon, Tue, Wed, Thu, or Fri. If it is, then it matches pattern number 1 and will echo “today is a weekday.” If that does not match, it checks to see if the date is Sat or Sun. If it is, the script echoes “today is the weekend.” Lastly, in case there is a problem with the system and the date command returns some other kind of information, the wildcard will be matched and we will get a “date not recognized” result.

Closing Thoughts

In this tutorial, you learned how to use a conditional case statement in Bash scripting on a Linux system. The case statements work well at handling many different possible outcomes, making them more scalable than if statements.




We encourage you to copy some of our examples to your own computer, and try executing them. Change around the code as needed in order to put your own spin on some of the scripts. Once you are familiar with the syntax of a case statement, they will be easy for you to write.



Comments and Discussions
Linux Forum