Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x