In this tutorial, we analyze the conditional statements supported in Bash cases. We review practical examples and discuss when a Bash case … the esac statement is better used than a more traditional one. if .. then .. fi
statement.
Bash case .. Esac statements in bash
Available natively from the bash shell, conditional sentences of cases, formulated using the case
and esac
idioms (an idiom is a word or group of words, o una keyword, with established usage patterns; a way of expressing a computer language), one can construct a complex conditional on a case-by-case basis (hence the term case
) search and declaration of execution by case.
The search part of a case-based statement is sometimes called Please select, which leads to the term select statement / case (or vice versa), used in some other coding languages like VBA, to refer to the same type of programming paradigm. Another set of languages (how C #, C ++, Java and JavaScript) they call this a change O switch / case statement. All of these have similar meanings and operations.
Let's build our first case .. esac
declaration using a simple script.
A simple case .. Esac text
We define our test script test.sh
as follows:
#!/bin/bash
case "${1}" in
1) echo "Option was 1!";;
2) echo "Option was 2!";;
*) echo "Option was something else: '${1}'";;
Esac
Then, we make our script executable by running chmod +x test.sh
and then we run 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 a variable to examine. Our case .. esac
declaration will verify each option (as indicated in the text preceding a )
idiom closing clause) and later process the code block (after the )
idiom) to get 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 ending idiom ;
(unless it's already at the end of a line), but within the case .. esac
statement, we need an additional ;
to indicate that we are finishing a code block for a specific case code execution block.
We can see how we have defined the code for both cases where 1
and 2
se pasan al script, respectively. Despite this, What happens when we spend something else? Then, we want to jump to our other clause, indicated by a universal *
idiom.
We see how we pass 1
O 2
al script, the correct action is taken respective; Option was 1/2!
. We also see how if we do something else, the *
The code block executes correctly, and also shows our first variable passed to the script. What if we didn't pass any option to our script? the case .. esac
the code would still be evaluated, and since nothing matches except for our catch-all *
clause, that runs:
Then, What if we assumed, something that may seem logical, that we can leave a single ;
declaration of the end of the line of each case .. esac
clause simply because an EOL is satisfied at the same time (end of line)?
Please note that only one termination ;
was used in this script. Bash does not make it possible and an error occurs; the final termination syntax of ;;
is mandatory, and something to pay attention to when writing case .. esac
statements
Why case .. Esac And no and?
We could also write the above code using a if
statement:
#!/bin/bash if [ "${1}" == "1" ]; then echo "Option was 1!" elif [ "${1}" == "2" ]; then echo "Option was 2!" else echo "Option was something else: '${1}'"; be
This seems comparable in code quality and brevity compared to our case .. esac
code, and in given situations and specific use cases like these, often a if .. then .. elif .. else .. fi
The based response will work well.
Having said this, if and when the complexity of the selection becomes more complex or greater, or if there are multi-layer case selections to perform (note that case .. esac
can be nested), may be worth using 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, despite this, this can also be done with standard bash if
statements, using as an example the -a
(and and -o
(u) options of our test clause and specifying a secondary test clause. As an example, if [ "${1}" -eq 1 -o "${1}" -eq 3 ]; then ...
Note that here we also use numerical comparisons instead of text-based comparisons as in our previous example.
For more information on if .. then .. else .. elif .. fi
conditionals, You can also read our post Conditional tests in Bash: if, then, else, elif.
Expanding our case
Let's extend our script above and make our conditions a bit more complex:
#!/bin/bash
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'!";;
Esac;;
*) echo "Option was something else: '${1}'";;
Esac
In this circumstance, We have listed two possible options for each code block and have separated them by a |
separator. Notice how everything works fine and the inputs 1
and 3
are correctly analyzed. In the second code block, we have created a nested case and use every feasible input to the second code block (namely 2
and a
) as new options in the nested esac / high school.
This again works perfectly. The flow through the code is that in the first case
statement, the 2|'a'
the branch is taken 2
O a
It happens, and later the second case
ensures that either 2
O a
is individually selected. Also of interest is the use of ;;
every time we want to finish a block of code for a specific option, and this also applies to the termination of the nesting case .. esac
, again with ;;
, In other words esac;;
.
Ending
In this tutorial, we analyze practical examples of bash based case .. esac
statements. We also saw where case statements are a better option than the more traditional ones.. if .. then .. fi
supported statements.
Si Bash le interesa, can find primer: Bash Loops: for, while and up, Bash Process Termination Hacks and How to Correctly Parse File Names in Bash.
Enjoy!