Skip to content
Log In Sign Up info@muvcloud.com

The Syntax

case "$value" in
  pattern1)
    commands
    ;;
  pattern2|pattern3)
    commands
    ;;
  *)
    commands for anything else
    ;;
esac

Bash compares the word after case with each pattern from the top, runs the commands of the first match, and continues after esac, which is case spelled backward. Each branch ends with ;;. There is no switch keyword in Bash; case is the statement for that job.

A First Example

#!/bin/bash
read -r -p "Restart nginx now? [y/n] " answer

case "$answer" in
  y|Y|yes)
    sudo nginx -t && sudo systemctl restart nginx
    ;;
  n|N|no)
    echo "Nothing changed."
    ;;
  *)
    echo "Please answer y or n."
    exit 1
    ;;
esac

The vertical bar separates alternative patterns in one branch. In the first branch, the configuration test runs first, and the restart happens only if the test passes.

Patterns Are Wildcards, Not Regular Expressions

case patterns use the same wildcards as file names:

PatternMatches
*.logAnything ending in .log
[Yy]*Anything starting with y or Y
???Exactly three characters
[0-9]*Anything starting with a digit
start|beginEither word
*Anything at all

Quoted parts of a pattern match literally: "*.log" matches only the text *.log itself. Quote the variable after case out of habit, as everywhere in Bash. To compare without regard to case, run shopt -s nocasematch before the case block.

The Default Branch

*) matches anything, so it belongs last: a branch after it never runs. Use it to print a usage line and exit with a nonzero status. Without a default branch, a value that matches nothing runs no commands, and case itself returns status 0, so the script carries on as if all were well.

How a Branch Ends

  • ;;: stops; Bash continues after esac. This is what you want almost always.
  • ;&: falls through and runs the commands of the next branch without testing its pattern.
  • ;;&: keeps testing the remaining patterns and runs every further match.

The last two exist in Bash 4.0 and later, which every current distribution ships. A script that uses them must run with bash, not sh.

A Script That Takes Commands

#!/bin/bash
# Usage: ./site.sh start|stop|restart|reload|status|logs [lines]
service="nginx"

case "$1" in
  start|stop)
    sudo systemctl "$1" "$service"
    ;;
  restart|reload)
    sudo nginx -t && sudo systemctl "$1" "$service"
    ;;
  status)
    systemctl status "$service" --no-pager
    ;;
  logs)
    sudo journalctl -u "$service" -n "${2:-50}" --no-pager
    ;;
  -h|--help|"")
    echo "Usage: $0 start|stop|restart|reload|status|logs [lines]"
    ;;
  *)
    echo "Unknown command: $1" >&2
    exit 1
    ;;
esac

Save it as site.sh, make it executable with chmod +x site.sh, and run ./site.sh status or ./site.sh logs 100. The pattern "" catches a call with no argument, and ${2:-50} uses 50 lines when no second argument is given. More systemctl and journalctl commands are in the Linux commands cheat sheet.

case vs if

Use case when you compare one value with several fixed strings or wildcard patterns: it is shorter and easier to read. Use if for numbers, file tests such as -f, combined conditions, and regular expressions. The same check written with if:

#!/bin/bash
if [[ "$1" == "start" || "$1" == "begin" ]]; then
  echo "Starting"
elif [[ "$1" == *.log ]]; then
  echo "Log file"
fi

Common Mistakes

  • Missing ;;: Bash stops with a syntax error near the closing parenthesis of the next pattern. Only the last branch before esac may leave it out.
  • Default branch first: *) catches everything, and later branches never run.
  • Regular expression syntax: [0-9]+ means a digit followed by a plus sign, not one or more digits.
  • Running with sh: on Debian and Ubuntu, sh script.sh uses dash, which lacks Bash features such as shopt. Run bash script.sh or use the shebang; see types of shells in Linux.

Loops pair well with case; see continue and break in Bash loops.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations