DEV Community

Amanda Igwe
Amanda Igwe

Posted on

2 1 1 1

Day 30/ 30 Days of Linux Mastery: Mastering 'for', 'do', 'while', and 'case' Statements in Shell Scripts

Table of Contents


Introduction

Welcome back to Day 30 of this practical Linux challenge! Part 4/4 of our beginner shell scripting series for RHEL 9.

If you have ever wanted your Linux system to repeat tasks, check conditions, or handle input differently based on options, then you are ready to meet three of the most powerful tools in shell scripting: for loops, do, while loops, and case statements.


What Are Loops and Case Statements?

Loops help you repeat commands without having to type them repeatedly.

Case statements help you respond to different inputs, like choosing a different action depending on the value a user gives.

In Bash, we use:

  • for loop: repeat something a certain number of times
  • do: Starts the loop block
  • while loop: repeat while something is true
  • case: handle multiple possible inputs

Core Loops and Case Statements Syntax

  • The for and do statement syntax;
for item in 1 2 3
do
   echo "Number: $item"
done

Enter fullscreen mode Exit fullscreen mode

What they do;

  • for item in 1 2 3: This means we are looping through the numbers 1, 2, and 3.
  • do: This begins the block of code we want to repeat.
  • done: This ends the loop block. The loop knows to stop after this.

Think of do as “start the loop task” and done as “finish the loop task.


  • The while loop statement syntax;
count=1
while [ $count -le 3 ]
do
   echo "Count is $count"
   ((count++))
done

Enter fullscreen mode Exit fullscreen mode

What they do;

  • while [ $count -le 3 ]: This checks if count is 3 or less.
  • do: Starts the loop block.
  • ((count++)): Adds 1 to count each time.
  • done: This ends the loop block. The loop knows to stop after this.

So, the loop runs until count becomes 4.


  • The case statement syntax;
echo "Enter a fruit:"
read fruit

case $fruit in
   apple)
      echo "Apples are red or green.";;
   banana)
      echo "Bananas are yellow.";;
   *)
      echo "Unknown fruit.";;
esac
Enter fullscreen mode Exit fullscreen mode

What they do;

  • read fruit: User types a fruit name.
  • case $fruit in: Begins checking value.
  • apple), banana): These are the options.
  • *): Wildcard — matches anything else.
  • ;;: Ends each option’s command
  • esac : simply means case spelt backward. Used to end a case statement.

Real-World Scenario: Loops and Case Statements

Let’s write a for loop script that checks for a file and let us know if it exists or not.

  • Open your terminal, create a file and add this line into it
vim forloop.sh   # note that script files end with a .sh

#!/bin/bash

# FOR LOOP EXAMPLE


echo "FOR loop - Print numbers 1 to 5:"
for i in {1..5}
do
  echo "Number: $i"
done

# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

forloop2 description

  • Now, give the script permission to run
chmod +x forloop.sh     # this enables it to be executable
Enter fullscreen mode Exit fullscreen mode
  • Verify the file has the execute permissions
ls -ltr forloop.sh
Enter fullscreen mode Exit fullscreen mode
  • Run the script on your terminal
./forloop.sh
Enter fullscreen mode Exit fullscreen mode

forloop1 description

  • Let's practice the while scenario
  • Create a script file, paste the code, or you can write your own now, then grant it the executable permissions.
#!/bin/bash


# While loop Example


echo " WHILE loop - Count down from 3:"
count=3
while [ $count -gt 0 ]
do
  echo "Countdown: $count"
  count=$((count - 1))
done


# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

while2 description

while1 description


  • Let's practice the case scenario
  • Create a script file, paste the code, or you can write your own now, then grant it the executable permissions.
#!/bin/bash

echo "CASE statement - Choose an option:"
echo "Enter a fruit: apple, banana, or mango"
read fruit

case $fruit in
  apple)
    echo "You chose Apple"
    ;;
  banana)
    echo "You chose Banana"
    ;;
  mango)
    echo "You chose Mango"
    ;;
  *)
    echo "Unknown fruit. Please pick from apple, banana, or mango."
    ;;
esac

# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

case1 description

case2 description


Conclusion

And that’s it!

You have now learned how to use for, do, while, and case statements in your shell scripts. These tools help you make your scripts smarter, more flexible, and powerful.

Take your time to practice each one, experiment on your terminal, and slowly build confidence. The more you use them, the easier it becomes.

Keep going, you are doing great!

If this is helpful to you, feel free to bookmark, comment, like and follow me!


Let's Connect!

If you want to connect or share your journey, feel free to reach out on LinkedIn.
I am always happy to learn and build with others in the tech space.

#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

Top comments (0)

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!