How to write For loop in Shell

 

A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.

for loop syntax

Numeric ranges for syntax is as follows:

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done
This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:
#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done
Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:
#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).
for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done
A representative three-expression example in bash as follows:
#!/bin/bash
for (( c=1; c<=3; c++ ))
do
	echo "Welcome $c times..."
done
Sample output:
Welcome 1 times
Welcome 2 times
Welcome 3 times

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:
#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

 

FUNFACT: When we try to remember something, it’s actually a for loop re-iterating within our brain, until we found the matching or desired result 🙂

Advertisements

About Swapratim

Blog Writer in Medium for Chatbots Magazine - world's biggest chatbot magazine. Also he writes in Bot Publication and Chatbots Journal on regular basis (https://medium.com/@swapratimroy). Experienced in IT development & support pursuing different roles & responsibilities. He has worked on several innovative projects. Android apps developed by him – In Time SMS, AndroLogic which are freely available in Google Play Store. He has several in house tools built available for free usage on internet. Apart from this, he likes to share his learning with everyone which he had learned over years. Sole purpose of this blog is to share the author's knowledge in IT, passion for travel and recent trends with you. Hope you will enjoy his contents.
This entry was posted in Various Scripting Tutorials. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s