Using Case Statements in Bash

When writing Bash scripts it is common to check a string against a variable and then do something based on what was specified.

Consider the following script. It checks the first argument that a user specifies and does different things depending on what was given.

This demonstrates albeit with no functionality a simple task that can repeat a known shape back to a user or if the shape is unknown show an error message.

Everythingaposs fine and dandy in this and it all works as expected. But look at how many lines that took. See how you have to handle every single option that a user passes through an elif statement.

Now imaging this program has even more options say 20. With the current approach you are going to be writing an if statement with 20 elif sections inside it.

Yeah youaposll probably be able to read it for the most part. But itaposs clunky takes up too much space and most importantly there is a better alternative.

...

Read Full Post

News Link: https://linuxhandbook.com/bash-case-statement/.

Linux Chatter is a news aggregator service that curates some of the best Linux, Cloud, Technical Guides, Hardware and Security news. We display just enough content from the original post to spark your interest. If you like the topic, then click on the 'read full post' button to visit the author's website. Use Linux Chatter to find content from amazing authors!

Note: The content provided has been modified and is not displayed as intended by the author. Any trademarks, copyrights and rights remain with the source.

Disclaimer: Linux Chatter sources content from RSS feeds and personal content submissions. The views and opinions expressed in these articles are those of the authors and do not necessarily reflect those of Linux Chatter.