3.3 Pattern Matching

Authored by Atri

Case Statements

Control flow is able to be managed effectively by if statements as discussed here. Though, sometimes there may be cases where you want a more flexible option for handling command execution. This is where case statements come into play; they offer a much more flexible workflow compared to your typical if statement.

To define a case statement, you can define your logic like so:

example="hello"

case $example in
	world!)
		echo "Branch A"
		;;
	hello)
		echo "Branch B"
		;;
	*)
		echo "Branch C"
		;;
esac
Note

You must use ;; at the end of each pattern block; this is not a typo. In Bash, this syntax functions as the analog for the break keyword in equivalent languages when used in the context of a case statement.

Case statements also support simple union declarations per-pattern:

example="hello"

case $example in
	hello | world!)
		echo "Branch A"
		;;
	*)
		echo "Branch B"
		;;
esac

If the need arises, it is also possible to perform expression matching when a simple pattern is defined as the statement:

example="hello"

case $example in
	h*)
		echo "World!"
		;;
	*)
		echo "This should not execute"
		;;
esac
Warning

While it may seem at a glance that you can use Regular Expressions with case statements, it does not appear that they function exactly the same as they do in other languages. Care should be taken to ensure your expressions are behaving exactly as intended.