• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to use Case Statement on Bash Script

  • 00:44 cat /etc/os-release
  • 01:00 vim casestatement
  • 05:14 chmod +x casestatement
  • 05:24 ll
  • 05:34 ./casestatement
  • 05:50 ./casestatement
  • 06:05 ./casestatement
{{postValue.id}}

To use case Statement on Bash Script ?

Introduction:

A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an edge over if-statements because it improves the readability of our code and they are easier to maintain.

Installation Steps:

Step1: Check the Oracle Linux Version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2

Step 2: Next create a one script file for Case Statement by using Vim command

[root@linuxhelp ~]# vim casestatement
#!/bin/bash
echo -n "Are you a Student  [Yes] or [No] : "
read response
case $response in

        "Y" | "y" | "yes" | "Yes" | "YES" )
        echo -n "Yes,I am a student"
        ;;
        "N" | "n" | "no" | "No" | "NO" )
        echo -n "No,I am not a student"
        ;;
        *)
        echo -n "Invalid Response"
        ;;
esac

Step 3: Execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x casestatement

Step 4: List the file by using the following commands

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  345 Sep 25 16:47 casestatement

Step 5: Execute the script by using the following command

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : y
Yes,I am a student[root@linuxhelp ~]#

Step 6: Again, execute the script by using the following command.

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : n
No,I am a student[root@linuxhelp ~]#

Step 7: Again, execute the script by using the following command

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : t
Invalid Response[root@linuxhelp ~]#

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use case Statement on Bash Script. Your feedback is much welcome.

Tags:
jacob
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

Give the purpose of the shebang line.

A

The shebang line is at the top of each script used to determine the engine's location and is used to execute the script.

Q

What is the alternative command for echo?

A

The alternative command for echo is tput. This command allows us to control how the output is displayed on the screen.

Q

How to run multiple bash script in parallel?

A

Multiple bash scripts can be executed in parallel by using nohup command. How multiple bash files can be executed in parallel from a folder is shown in the following example.

Q

What are the advantages of using bash scripts?

A

• It is easy to use and learn.
• Many manual tasks that need to run frequently can be done automatically by writing a bash script.
• The sequence of multiple shell commands can be executed by a single command.

Q

Mention the disadvantages of bash scripts

A

• Some disadvantages of bash script are mentioned below:
• It works slower than other languages.
• The improper script can damage the entire process and generate a complicated error.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luke ?
workbench for debian

I am using workbench in CentOS whereas now I need to use Debian Operating system so could you please help to install and use in Debian?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.