Case Statement

Learn how the case statement works.

We'll cover the following...

A program that follows a conditional algorithm chooses its actions depending on the values of variables. If some variable has one value, the program does one thing. Otherwise, it does something else. The conditional statements of programming languages provide such behaviors.

We considered the if statement. There is another conditional statement in Bash called case. It’s more convenient than if, in some cases.

Document archiving script example

An example will help us to compare if and case statements. Let’s suppose that we write a script for archiving documents. The script has three operating modes:

  1. Archiving without compression.
  2. Archiving with compression.
  3. Unarchiving.

We can choose the mode by passing a command-line option to the script. The following table shows the possible options:

...