in

LinuxLinux

Bash Case Statement Command Syntax & Examples in Linux {Latest}

Quick Tutorial about Bash Case Statement in Ubuntu Linux

Bash Case Statement

B

ash Case Statement: Case statements are one of the main function of basic programming. If you want to perform a multiple option function, Then Bash Case is one of the best way to achieve! This Bash Case statment replaces the Nested if else function. The Base Case Statement is the efficient one & readable, easy to maintain. In this tutor, we provided the basic usages of Bash Case Statments with Syntax & Examples.

Jump To:

Bash Case Statement Command Syntax:

The basic bash case statement command syntax is

Bash Case Statement Syntax
case EXPRESSION in

  YOUR_PATTERN_1)
    YOUR_STATEMENT
    ;;

  YOUR_PATTERN_2)
    YOUR_STATEMENT
    ;;
    .
    .
    .
    .
    .
  YOUR_PATTERN_N)
    YOUR_STATEMENT
    ;;
   
  *)
    DEFAULT_STATEMENT
    ;;
esac

Bash Case Statement Syntax Explnation:

  • CASE & ESAC– Each case statment must start with CASE keyword followed by your condition &  followed by in keyword. The CASE statement must be end with ESAC keyword.
  • You can use more than one pattern in the same case using the or | operator. The patterns should be ends with parentheses ).
  • You can add special character in your patterns.
  • The pattern with its statement is called as the Clause.
  • You need to end your_patterns with the Double-Semicolon ;;
  • The Default statement can be declared through the * Asterisk symbol. You should use the * symbol followed by parentheses ), which terminates the statement.
  • The command which matches with the patterns & the corresponding statement will be executed.
  • If your command matches no pattern & then the result status will exit the commands.
Check this too  ls Command: How To List Files in Linux Using ls Command

Bash Case Statement Command Examples:

The below code is the example for Bash Case Statement Command,

number.sh
#!/bin/bash

echo -n "Which number set that you need to print: "
read NUMBER

echo -n "The Number Set of  $NUMBER is "

case $NUMBER in

  even)
    echo -n "2,4,8,10,12,14,18,20"
    ;;

  odd)
    echo -n "1,3,5,7,9,11,13,15,17,19"
    ;;

  prime)
    echo -n "1,3,5,7,11,13,17,23,29"
    ;;

  *)
    echo -n "Wrong Number Set"
    ;;
esac

The output of the above bash case statement command is,

Output
Which number set that you need to print: odd
The Number Set of odd is 1,3,5,7,9,11,13,15,17,19
If you no pattern matches with the above command it will print the default statement. The default output of the above statement is,
Output
Which number set that you need to print: whole
The Number Set of whole is Wrong Number Set

Verdict:

That’s it, The above tutorial will help you understant the concept of Bash Case Statement command in Linux, If you have any queries and then comment us below.

Article Review
  • Content Explanation
  • Case Statement Syntax
  • Case Statement Example
4.9

Summary

For more article, Kindly visit our Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

Install Chromium Linux Ubuntu

Chromium Linux- How to Install Chromium on Linux Ubuntu 18.04.2 LTS, 19.04 & More

ICQ-Instant Messenger Ubuntu: Install ICQ Instant Messenger for Ubuntu 18.04 LTS and 19.04