Case/switch statements and loop constructs

Besides if and else statements, Bash offers case or switch statements and loop constructs that can be used to simplify logic so that it is more readable and sustainable. Imagine creating an if statement with many elif evaluations. It would become cumbersome!

#!/bin/bashVAR=10# Multiple IF statementsif [ $VAR -eq 1 ]; then    echo "$VAR"elif [ $VAR -eq 2]; then    echo "$VAR"elif [ $VAR -eq 3]; then    echo "$VAR"# .... to 10else    echo "I am not looking to match this value"fi
In a large number of blocks of conditional logic of if and elifs, each if and elif needs to be evaluated before executing a specific branch of code. It can be faster to use a case/switch statement, because the first match will be executed ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.