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 for loop



Loopsย are a programming construct which allow us to repeat a command or set of commands for each item in a list. As such they are key to productivity improvements through automation. Similar to wildcards and tab completion, using loops also reduces the amount of typing required (and hence reduces the number of typing mistakes).

Syntax of For Loop

Example of For Loop

There are some key points of ‘for loop’ statement:

  • Each block of ‘for loop’ in bash starts with ‘do’ keyword followed by the commands inside the block. The ‘for loop’ statement is closed by ‘done’ keyword.
  • The number of time for which a ‘for loop’ will iterate depends on the declared list variables.
  • The loop will select one item from the list and assign the value on a variable which will be used within the loop.
  • After the execution of commands between ‘do’ and ‘done’, the loop goes back to the top and select the next item from the list and repeat the whole process.
  • The list can contain numbers or string etc. separated by spaces.

How For Loop works?

Write Simple Shell Script using For Loop


#!/bin/bash
for i in 1 2 3 4 5
do
 echo "Hello $i"
doneCode language: PHP (php)

Letโ€™s inspect each element:

  • #!/bin/bash โ€“ shows that the code is a bash script
  • i โ€“ is a placeholder for a variable. Meanwhile, $i is the individual value of the variable. You can also write it as c/$c or by any other name
  • in โ€“ separates the variable and the items that follow
  • 1 2 3 4 5 โ€“  is an example of items you want to perform the instruction on
  • do โ€“ is the keyword that starts the loops. It will then execute the instruction n times, with n being the total number of items. Here, the value of n is 5
  • echo โ€œHello: $iโ€ โ€“ is the code which we will repeat n times. Remember quotation marks turn anything inside it into one variable.
  • done โ€“ stops the loop

Example Program

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