Case statement
Simple case statement
Section titled “Simple 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=1case $var in1) echo "Antartica" ;;2) echo "Brazil" ;;3) echo "Cat" ;;esacOutputs:
AntarticaCase statement with fall through
Section titled “Case statement with fall through”Since bash 4.0, a new operator ;& was introduced which provides fall through mechanism.
#!/bin/bash
var=1case $var in1) echo "Antartica" ;&2) echo "Brazil" ;&3) echo "Cat" ;&esacOutputs:
AntarticaBrazilCatFall 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=abccase $var ina*) echo "Antartica" ;;&xyz) echo "Brazil" ;;&*b*) echo "Cat" ;;&esacOutputs:
AntarticaCatIn the below example, the abc matches both first and third case but not the second case. So, second case is not executed.