Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Shell Scripting Tutorials: Looping & Iteration statatement using while


The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.

Most languages have the concept of loops: If we want to repeat a task twenty times, we don’t want to have to type in the code twenty times, with maybe a slight change each time.
As a result, we have for and while loops in the Bourne shell.


Types of Loops in Shell Scripting



Syntax of Shell Scripting



Control Flow of While Loop


Example of While Loop in Shell Script



Boolean Operators When Working With Shell Scripts


Apart from the boolean operators that you’ve seen in other programming languages, including ==, <, >, <=, >=, etc., these are shell script specific operators. The major usage difference is that these operators will only work when numeric values are provided. With string value, the operation will simply fail.

OperatorDescriptionExample
-neChecks if both operands are not equal[ 1 -ne 2 ] is TRUE
eqChecks if both the operands are equal[ 1 -eq 2 ] is FALSE
ltCheck if the left operand is lesser than the right operand[1-lt 2 ] is TRUE
gtCheck if the left operand is greater than the right operand[1-gt 2 ] is FALSE
leCheck if the left operand is less than or equal to the right.[1-le 2 ] is TRUE
geCheck if the left operand is greater than or equal to the right.[1-ge 2 ] is FALSE

Creating a While Loop in Shell Script

A while loop doesn’t have a repetition limit by default. We have to explicitly provide it a condition that at some point during the code execution, will turn to false. Else you’ll have a loop that doesn’t end unless an interrupt signal is sent.

Save the script above by any name with the .sh extension. To run the file, you can either run it with the bash command like bash <filename>.sh or make the file executable using the chmod command. To make the script executable, run chmod +x <filename>.sh and then ./<filename>.sh will allow you to run the script.

Let’s understand the script line by line.

  • i=0 – Here we set the variable $i to 0. Learn more about variables in our previous tutorial
  • while [ $i -le 10 ] – Run the while loop only until the variable $i is lesser than or equal to 10. So this loop will run 11 times including the ZERO-th run.
  • do – Marks the beginning of the while loop in shell scripts
  • echo True – A simple command that will print the word “True” on our terminal
  • ((i++)) – C style variable increments to add 1 to the variable i with every loop. There are various other ways to perform an increment include $i=((i+1)), ((i=i+1)), etc. Feel free to use either one.
  • done – Marks the end of the while loop
  • echo False – This again simply prints out the word False on the screen to indicate that condition has now turned false.

Example of nested While Loop


Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Top 10 Disk Partition Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, disk partitioning is crucial for maintaining an efficient and organized storage system. Disk partition tools are essential for creating, resizing, merging, splitting, and managing…

Read More

Top 10 Backup & Recovery Tools in 2026: Features, Pros, Cons & Comparison

Introduction In today’s digital world, data is the backbone of virtually every business operation. As the amount of critical data grows, so does the need for robust…

Read More

Top 10 Barcode Generation Tools in 2026: Features, Pros, Cons & Comparison

Introduction Barcode generation tools are essential for modern business operations. They allow businesses to create scannable codes that represent information such as product IDs, prices, or inventory…

Read More

Top 10 Cloud Backup Tools in 2026: Features, Pros, Cons & Comparison

Introduction Cloud backup tools are indispensable in 2026 for businesses and individuals seeking to safeguard their data in an increasingly digital world. These tools provide remote, secure…

Read More

Top 10 Data Recovery Tools in 2026: Features, Pros, Cons & Comparison

Introduction In today’s digital world, data is one of the most valuable assets for businesses and individuals alike. Whether you’re dealing with business-critical files or personal memories,…

Read More

Top 10 Data Replication Tools in 2026: Features, Pros, Cons & Comparison

Introduction Data replication refers to the process of copying data from one location to another to ensure consistency and availability across multiple systems. As businesses increasingly rely…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x