Case in shell scripting.

We have used if loop and while loop multiple times and without it no bash script has been completed. But at a times using those loops are very much helpful and you have to use CASE. Let’s see more about case in shell scripting

Below is the sample code, which will print the capital after you enter the country name. Just try to write the same code in If loop, It would be very long, untidy and hard to manage.

echo -n "Enter the Country name "
read Country

echo -n "Captial of $Country is "

case $Country in
        India )
       echo -n "Delhi"
       echo ""

    Argentina )
     echo -n "Buenos Aires"
      echo ""

    Brazil )
     echo -n "Brasília"
      echo ""

    * )
     echo -n "unknown"


Let’s try to run the above code. It will ask you to first entry the country name it will print it’s capital and if the entry is not mentioned it will just print unknown.

[root@server ~]# ./
Enter the Country name India
Captial of India is Delhi

[root@server ~]# ./
Enter the Country name Argentina
Captial of Argentina is Buenos Aires

[root@server ~]# ./
Enter the Country name USA
Captial of USA is unknown
[root@server ~]#

We have bunch of information available on scripting.

Leave a Comment