Popular Searches

How to Use case .. esac Statements in Bash

Bash Shell

In this tutorial we look at Bash case based conditional statements. We review practical examples, and discuss when a Bash case .. esac statement is best employed over a more traditional if .. then .. fi statement.

Bash case .. esac Statements in Bash

Natively available from within the Bash shell, case conditional statements, formulated using the case and esac idioms (an idiom is a word or a group of words, or a keyword, with established usage patterns; a form of expressing a computer language), one can construct a complex conditional case-by-case (hence the term case) lookup and execution-per-case statement.

The lookup part of a case based statement is sometimes called a select, leading to the term select/case statement (or the reverse), used in some other coding languages like VBA, to refer to the same type of programming paradigm. Yet another set of languages (like C#, C++, Java and JavaScript) call this a switch or switch/case statement. All these have similar meanings and operations.

Let’s build our first case .. esac statement using a simple script.

A simple case .. esac script

We define our test script test.sh as follows:


case "${1}" in 
  1) echo "Option was 1!";;
  2) echo "Option was 2!";;
  *) echo "Option was something else: '${1}'";;

Next, we make our script executable by executing chmod +x test.sh and then we execute the script itself, each time passing a different option as input.


In our case opening clause, we specify that we want to use the first input option ($1) as the variable to be evaluated. Our case .. esac statement will verify each option (as indicated by the text preceding a ) clause closing idiom) and then process the code block (after the ) idiom) for the correct result.

Note that there are two ; end-of-statement terminators at the end of each line. The reason for this is simple; normal Bash syntax requires you to have at least one terminating idiom ; (unless you are at the end of a line already), but inside the case .. esac statement, we need an additional ; to indicate that we are terminating a code block for a specific case code execution block.

We can see how we have defined code for both the cases where 1 and 2 are passed to the script, respectively. However, what happens when we pass something else? In that case, we want to jump to our other clause, indicated by a universal * idiom.

A simple case .. esac statement in Bash

We see how we pass 1 or 2 to the script, the corresponding correct action is taken; Option was 1/2!. We also see how if we do something else, the * code block is executed correctly, and nicely our first passed variable to the script is displayed also. What would happen if we passed no option to our script? The case .. esac code would still be evaluated, and since nothing matches except for our catch-all * clause, that one is executed:

Not passing a variable to our test script also works due to the * clause in our case .. esac statement

So what would happen if we were to assume, something which may seem logical, that we can leave off a single ; statement from the end of the line of each case .. esac clause simply because at the same time an EOL (end of line) is reached?

Incorrect code block termination in a Bash case..esac statement

Note that only a single terminating ; was used in this script. Bash does not allow this, and an error is produced; the final terminating syntax of ;; is required, and something to keep in mind when writing case .. esac statements

Why case .. esac And Not if?

We could also write out the previous code using an if statement:


if [ "${1}" == "1" ]; then
  echo "Option was 1!"
elif [ "${1}" == "2" ]; then
  echo "Option was 2!"
  echo "Option was something else: '${1}'";

An alternative to a case..esac statement using an if..then..elif..else..fi statement


This looks comparable in code quality and shortness in comparison with our case .. esac code, and in the given situations and specific use cases like these, often an if .. then .. elif .. else .. fi based solution will do just fine.

That said, if and when the complexity of the selection becomes more complex or larger, or if there are multi-layered case selections to be performed (note that case .. esac can be nested), it may pay off to use case .. esac statements instead.

Another nice feature of case .. esac statements is that one can list two possible options for a single code execution block, as we will see in our next example, however this an also be done with standard Bash if statements, using for example the -a (and) and -o (or) options from within our test clause and specifying a secondary test clause. For example, if [ "${1}" -eq 1 -o "${1}" -eq 3 ]; then ... Note that here we also used numerical comparisons instead of text based comparisons as in our example above.

For more information on if .. then .. else .. elif .. fi conditionals, you can also read our article Conditional Testing in Bash: if, then, else, elif.

Expanding Our Case

Let us expand on our previous script and make our conditions a bit more complex:


case "${1}" in 
  1|3) echo "Option was 1!"
       echo "Or, option was 3!";;
  2|'a') case "${1}" in 
           2) echo "Option was 2!";;
         'a') echo "Option was 'a'!";;
  *) echo "Option was something else: '${1}'";;

A more complex case .. esac statement in Bash with additional selectors


In this case, we have listed two possible options for each code block and separated them by a | separator. Note how everything works well and the inputs 1 and 3 are correctly parsed. In the second code block we have created a nested case, and used each possible input to the second code block (namely 2 and a) as new options in the nested/secondary esac.

This again works flawlessly. The flow through the code is that in the first case statement, the 2|'a' branch is taken is 2 or a is passed, and then the second case ensures that either 2 or a is selected individually. Of interest is also the use of ;; every time we want to terminate a code block for a specific option, and this also applies to terminating the nested case .. esac, again with ;;, i.e. esac;;.

Wrapping up

In this tutorial, we looked at practical examples of Bash based case .. esac statements. We also saw where case statements are a better choice then more traditional if .. then .. fi based statements.

If Bash interests you, you may find Primer: Bash Loops: for, while, and until, Bash Process Termination Hacks and How to Correctly Parse File Names in Bash to be of interest.


Roel Van de Paar Roel Van de Paar
Roel has 25 years of experience in IT & business, 9 years of leading teams, and 5 years in hiring & building teams. He worked for companies like Oracle, Volvo, Sun, Percona, Siemens, Karat, and now MariaDB in various senior, principal, lead, and managerial roles. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.