We can use Linux Bash For Loop for doing Linux administration day to day task. In this post, we will try to find various ways through which we can complete our task in bash script with For Loop. For Loop is used to repeat task with some condition, further we can also put some more condition within loop to have some more control on For Loop working.

For loop Syntax

 Normal for loop 
for i in LIST
do
conditions commands
conditions commands
done
================================
C programm style for loop 
for i (( condition ))
do
conditions commands
conditions commands
done

Let try to understand it with some examples, different ways to complete our task with FOR Loop or what things we need to take care with FOR Loop.

Static list FOR Loop

#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
echo -n "2 X $i=" ;echo "2*$i"| bc
done

With this code we can see there is For Loop that used to calculate Multiple of Two, We have used static list 1 to 10 for i variable. We have used that i for some task that were used to calculate in further code. There are some points that need to take care while using List in For Loop.
Important Points

1. for, in , do and done are keywords that always used in such loop where we used static list values. We should take extra care because without them your code may came out with some errors.

2. Always used list without any comma between items, without double or single quotes cover whole list. That should separate with space or tab.

3. Here i is used as variable name, you can used any other as well.

4. In body of Loop, we used some commands, condition, calculation etc. These are depend upon your requirements with code.

Varibles list for For Loop

In this we can ask variables from user or provide while running code, static variable provide in code itself

This code is refer in which user will asked for input
#!/bin/bash
read -p "Type Five Month's name :" month; echo ""
for i in $month
do
echo "Month name that you have entered is :$i"
done
Sample Output
#./for_2
Type Five Month's name :Jan Feb Mar Apr May
Month name that you have entered is :Jan
Month name that you have entered is :Feb
Month name that you have entered is :Mar
Month name that you have entered is :Apr
Month name that you have entered is :May
================================
This code is refer in which variables will provide at script running time
#!/bin/bash
for i
do
echo "Month name that you have entered is :$i"
done
Sample Output
#./for3.sh Jan Feb Mar Apr May
Month name that you have entered is :Jan
Month name that you have entered is :Feb
Month name that you have entered is :Mar
Month name that you have entered is :Apr
Month name that you have entered is :May
================================
This code is refer in which variables used within code
#!/bin/bash
month="Jan Feb Mar Apr May"
for i in $month
do
echo "Month name:$i"
Sample Outpu
#./for4.sh
Month name:Jan
Month name:Feb
Month name:Mar
Month name:Apr
Month name:May
done

Important points

1. We have used read function in bash for user input, this used many times in bash when we want to collect information from users while script execution time.
2. When we used to pass variable while running script, we used for i instead of for i in $month.
3. Whenever used variable within code, that should enclosed within double or single quotes as above mentioned code.

Linux commands output used as list in For Loop

Sometime there are situation when we need to use Linux commands as list for For Loop, Let’s see with examples

#!/bin/bash
for i in `awk -F: '{print $1}' /etc/passwd`
do
echo -n "Cron for User: $i -- "
crontab -l -u $i
done

C Style Syntax For Loop

Now we will talk about C Style Sntax For Loop, which covers same expression values as C does. So from where Loop start, where it ends and how it will increment or decrement decide in start of Loop. let’s try to understand with example.

#!/bin/bash
for (( a=1; a<=5; a++ ))
do
echo "Linux Random gentor: $RANDOM"
done
Sample Output
#./for5.sh
Linux Random gentor: 15243
Linux Random gentor: 22368
Linux Random gentor: 31530
Linux Random gentor: 6745
Linux Random gentor: 32428

Infinite For Loop

For Loop can be use as Infinite loop with C Style Syntax with empty expression. Here we need not to put any start, condition and increment in first line of Loop. To stop this Loop, we have to press ctl+c.

#!/bin/bash
for (( ;; ))
do
mpstat -P ALL
iostat -dchm
sleep 2
clear
done

This way we can create infinite Loop, that would only end with ctl+c.