Skip to content

Case statement

In its simplest form supported by all versions of bash, case statement executes the case that matches the pattern. ;; operator breaks after the first match, if any.

#!/bin/bash
var=1
case $var in
1)
echo "Antartica"
;;
2)
echo "Brazil"
;;
3)
echo "Cat"
;;
esac

Outputs:

Terminal window
Antartica

Since bash 4.0, a new operator ;& was introduced which provides fall through mechanism.

#!/bin/bash

Terminal window
var=1
case $var in
1)
echo "Antartica"
;&
2)
echo "Brazil"
;&
3)
echo "Cat"
;&
esac

Outputs:

Terminal window
Antartica
Brazil
Cat

Fall through only if subsequent pattern(s) match

Section titled “Fall through only if subsequent pattern(s) match”

Since Bash 4.0, another operator ;;& was introduced which also provides fall through only if the patterns in subsequent case statement(s), if any, match.

#!/bin/bash
var=abc
case $var in
a*)
echo "Antartica"
;;&
xyz)
echo "Brazil"
;;&
*b*)
echo "Cat"
;;&
esac

Outputs:

Terminal window
Antartica
Cat

In the below example, the abc matches both first and third case but not the second case. So, second case is not executed.