The case statement

When you are given more alternatives, you can process them with a sequence of if else statements:

if [condition];thencommandelsecommandfi

The if clauses can be nested if needed, but in the long run, having more than a few choices messes up the code, making it less readable. One of the basic mantras of coding is exactly keeping the code readable, making it elegant since elegance here does not simply mean beautiful, but also consistent over time. Always keep a meaningful indentation so that the clauses will outstand. Try to use as little code as you can, adopt the same notation all through your script, and make it all compact and lean. So, having a cascade of if/then/else/fi with a lot of indentation cannot show up as the ...

Get Mastering Bash 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.