# Guide: Using Case Statements in Bash Scripts

**URL:** https://www.compsmag.com/blogs/guide-using-case-statements-in-bash-scripts/
**Author:** Ayushi Chauhan
**Published:** 2021-12-20
**Updated:** 2021-12-20
**Categories:** Blogs
**Tags:** Blogs Daily, guiderobert, Guides and Tutorials, guiding17, How To Guide, Tips, Tricks
**Reading Time:** 2 min

---

This guide is about Guide: Using Case Statements in Bash Scripts. So read this free guide, Guide: Using Case Statements in Bash Scripts. If you have query related to same article you may contact us.

## Guide: Using Case Statements in Bash Scripts - Guide

Bash case statements are like if-else explanations, but they are more direct and less complex.  It helps to match a variable with some qualities.  It's used when you require IF-else statements with multiple joints... It's a lot like the switch proclamation in C, however, the slam case statement doesn't advance once the example is coordinated

## case statements

Case statements are used in decision making and are similar to multilevel if statements.  They are used ​​when you have multiple options in the form of a pattern match.  If you find yourself writing a long if ... else ... else ... else ... if multilevel ladder, you can replace it with a case statement.  Your code will be easier to read and maintain.

## Structure

> Case statements are structured as follows:
> case commands $ variable inpattern-1 ;;  pattern-2) commands ;;  pattern-3 |  pattern-4 |  pattern-5) commands ;;  pattern-N) commands ;;  *) commands ;;  esac

A case statement will selectively execute commands under the pattern that matches the value of $variable.  This means that the value of the $ variable will be compared against each pattern you specify before each closing bracket ')' in a top-down order.

The first successful match will cause commands under that matched pattern to execute up to the double semicolon ';;'  are found.  Then script execution will continue immediately after the esac statement, which marks the end of the case structure.

In case the variable $ is not matched by any of the patterns you specified, you can provide an optional catch-all pattern just before the final of the case.  This is done using the pattern '*)' and is equivalent to the else clause in an if…elif…else…fi statement.

The mandatory double semicolon in final of each set of commands for a pattern serve to instruct bash to ignore the rest of the case structure and jump to the final of the case marked by esac.

## Final note

I hope you like the guide Guide: Using Case Statements in Bash Scripts. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

---

*End of Article*