loop- For

for variable in list  
do  
commands  
done

------------------------

for (( expression1; expression2; expression3 ))  
do  
commands  
done

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.
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.

--------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE
--------------------------------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat>for1.sh

-- Script
#!/bin/bash

#This is the basic example of 'for loop'.
learn="Start learning from Javatpoint."

for learn in $learn
do
echo $learn
done
echo "Thank You."



[haritaraka12078@cxln4 ~]$ sh for1.sh
Start
learning
from
Javatpoint.
Thank You.



--------------------------------------------------------------------------------------------------------------------------------------
To print a series of numbers from 1 to 10
--------------------------------------------------------------------------------------------------------------------------------------


[haritaraka12078@cxln4 ~]$ cat>for2.sh

-- Script
#!/bin/bash
#This is the basic example to print a series of numbers from 1 to 10.
for num in {1..10}
do
echo $num
done
echo "Series of numbers from 1 to 10."



[haritaraka12078@cxln4 ~]$ sh for2.sh
1
2
3
4
5
6
7
8
9
10
Series of numbers from 1 to 10.



--------------------------------------------------------------------------------------------------------------------------------------
Read a Range with Increment/Decrement
We can increase or decrease a specified value by adding two another dots (..) and the value to step by,
--------------------------------------------------------------------------------------------------------------------------------------


-- Script
[haritaraka12078@cxln4 ~]$ cat>for3.sh

#!/bin/bash

#For Loop to Read a Range with Increment
for num in {1..5..1}
do
echo $num
done

#For Loop to Read a Range with Increment
for num in {5..0..1}
do
echo $num
done


[haritaraka12078@cxln4 ~]$ sh for3.sh
1
2
3
4
5
5
4
3
2
1
0


--------------------------------------------------------------------------------------------------------------------------------------
 Read Array Variables
array=(  "element1" "element 2" .  .  "elementN" )  
--------------------------------------------------------------------------------------------------------------------------------------
  
for i in "${arr[@]}"  
do  
echo $i  
done
  Each element could be accessed as 'i' within the loop for the respective iteration


[haritaraka12078@cxln4 ~]$ cat>for4.sh

-- Script
#!/bin/bash

#Example ======================================================
#Array Declaration
arr=( "Welcome""to""Javatpoint" )
for i in "${arr[@]}"
do
echo $i
done
echo "--------------------------------------"


#Example ======================================================
#For Loop to Read white spaces in String as word separators
str="Let's start
learning from Javatpoint."
for i in $str;
do
echo "$i"
done
echo "--------------------------------------"


# Example ======================================================
#For Loop to Read each line in String as a word
str="Let's start
learning from
Javatpoint."
for i in "$str";
do
echo "$i"
done
echo "--------------------------------------"


[haritaraka12078@cxln4 ~]$ sh for4.sh
WelcometoJavatpoint
--------------------------------------
Let's
start
learning
from
Javatpoint.
--------------------------------------
Let's start
learning from
Javatpoint.
--------------------------------------


--------------------------------------------------------------------------------------------------------------------------------------
Loop to Read Three-expression
--------------------------------------------------------------------------------------------------------------------------------------

Three expression syntax is the most common syntax of 'for loop'.
The first expression refers to the process of initialization,
 the second expression refers to the termination, 
 and the third expression refers to the increment or decrement.
 
 
 [haritaraka12078@cxln4 ~]$ cat>for5.sh

-- Script
#!/bin/bash

#For Loop to Read Three-expression
for ((i=1; i<=5; i++))
do
echo "$i"
done


[haritaraka12078@cxln4 ~]$ sh for5.sh
1
2
3
4
5


--------------------------------------------------------------------------------------------------------------------------------------
Loop with a Break Statement
--------------------------------------------------------------------------------------------------------------------------------------

-- Script
[haritaraka12078@cxln4 ~]$ cat>for6.sh

#!/bin/bash

#Table of 2
for table in {2..100..2}
do
echo $table
if [ $table == 20 ]; then
break
fi
done




[haritaraka12078@cxln4 ~]$ sh for6.sh
2
4
6
8
10
12
14
16
18
20



--------------------------------------------------------------------------------------------------------------------------------------
loop with ignoring
--------------------------------------------------------------------------------------------------------------------------------------

-- Script
[haritaraka12078@cxln4 ~]$ cat>for7.sh

#!/bin/bash
#Numbers from 1 to 20, ignoring from 6 to 15 using continue statement"
for ((i=1; i<=20; i++));
do
                if [[ $i -gt 5 && $i -lt 16 ]];
                then
                continue
                fi
echo $i
done



[haritaraka12078@cxln4 ~]$ sh for7.sh
1
2
3
4
5
16
17
18
19
20



--------------------------------------------------------------------------------------------------------------------------------------
Infinite Bash For Loop
--------------------------------------------------------------------------------------------------------------------------------------
When there is no 'start, condition, and increment' in the bash three expressions for loop, it becomes an infinite loop. To terminate the infinite loop in Bash, we can press Ctrl+C.

-- Script
#!/bin/bash  
  
i=1;  
for (( ; ; ))  
do  
sleep 1s  
echo "Current Number: $((i++))"  
done  
 

[prabhucloudxlab@cxln4 ~]$ cat>hello.sh

#! bin/bash
#syntax 1

for i in 1 2 3 4 5
do
    echo $i
done
echo "================"

#syntax 2
for i in {6..10}
do
    echo $i
done
echo "================"


#syntax 3
for i in {11..20..2} #{start..ending..increment}
do
    echo $i
done
echo "================"



#syntax 4
for (( i=21; i<25; i++ ))
do
    echo $i
done
echo "================"



[prabhucloudxlab@cxln4 ~]$ sh hello.sh
1
2
3
4
5
================
6
7
8
9
10
================
11
13
15
17
19
================
21
22
23
24