Bash Case – Syntax & Examples

Bash Case

In this Bash Tutorial, we shall learn the syntax and usage of Bash Case statement with example Bash Scripts.

Bash Case if similar to switch case in other programming lanugages.

Syntax of Bash Case

Syntax of Case statement in Bash Shell Scripting is as shown below :


EXPRESSIONwhere expression evaluates to a value.
CASENOne of the value the expression could evaluate to.
COMMAND-LISTSet of commands to be run when a CASE matches.

Note : Providing*  for a case can make the case to match to any value. This can be used as a default case when used as a last case.


Example 1 : Bash-Case Statement

In this example, we shall look into a simple scenario to illustrate case statement.

In the above example, the expression matches with second case 12 and therefore the commands for the corresponding case got executed.


Example 2 : Bash-Case statement with Default Case

In this example, we shall look into a scenario where there is a default case when no previous cases has a match.

The expression, does not match to any of the cases but last, default case.



In this Bash Scripting Tutorial, we have learnt about the syntax of usage of Bash Case statement with example bash scripts.