Bash For Loop Examples (For and Read-While Loops in Bash)

Today, We want to share with you bash for loop.In this post we will show you For and Read-While Loops in Bash, hear for for loop in unix shell script to read a file we will give you demo and example for implement.In this post, we will learn about bash tutorial with an example.

How to write a loop in Bash?

Bash supports the following looping constructs.

Loops in Bash

  • while
  • for
  • until

Basic syntax

for product in dog cat 'mobile elec' elephant ostrich
do
  echo "I want a $product for a iphone"
done
for data in $collection_of_things
do
  some_program $data
  another_program $data >> products.txt
  # as many commands as we want
done
for var_name in $(seq 1 100); do
  echo "Counting $var_name..."
done
for url in $(cat all_website_ulrs.txt); do
  curl "$url" >> full_sourcecode_online.html
done
while read url
do
  curl "$url" >> full_sourcecode_online.html
done < all_website_ulrs.txt

Basic 'For Loop' Example

Bash Script

#!/bin/bash  
#This is the basic example of 'for loop'.  
  
understand="Start understanding from Pakainfo."  
  
for understand in $understand  
do  
echo $understand  
done  
  
echo "Thank You."  

For Loop to Read a Range

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

For Loop to Read a Range with Increment/Decrement

For Increment

#!/bin/bash  
  
#For Loop to Read a Range with Increment  
  
for num in {1..10..1}  
do  
echo $num  
done  

For Decreament

Bash Script

#!/bin/bash  
  
#For Loop to Read a Range with Decrement  
  
for num in {10..0..1}  
do  
echo $num  
done  

For Loop to Read Array Variables

Bash Script

#!/bin/bash  
  
#Array Declaration  
arr=( "Welcome""to""Pakainfo" )  
  
for cnt in "${arr[@]}"  
do  
echo $cnt  
done  

Read a file, line-by-line, reliably with read-while

user@host:~$ for line in $(cat list-of-dirs.txt)
> do
>    echo "$line"
> done

For Loop to Read white spaces in String as word separators

Bash Script

#!/bin/bash  
#For Loop to Read white spaces in String as word separators  
  
websitename="Let's start  
understanding from Pakainfo."  
  
for cnt in $websitename;  
do  
echo "$cnt"  
done  

For Loop to Read each line in String as a word

Bash Script

#!/bin/bash  
#For Loop to Read each line in String as a word  
  
websitename="Let's start  
understanding from   
Pakainfo."  
  
for cnt in "$websitename";  
do  
echo "$cnt"  
done  

For Loop to Read Three-expression

Bash Script

#!/bin/bash  
#For Loop to Read Three-expression  
  
for ((cnt=1; cnt<=10; cnt++))  
do  
echo "$cnt"  
done  

For Loop with a Break Statement

Bash Script

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

For Loop with a Continue Statement

Bash Script

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

Infinite Bash For Loop

Bash Script

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

Loops for, while and until

For sample
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.

#!/bin/bash
for cnt in $( ls ); do
    echo product: $cnt
done

C-like for

#!/bin/bash
for cnt in `seq 1 10`;
do
        echo $cnt
done    

While sample

#!/bin/bash 
 COUNTER=0
 while [  $COUNTER -lt 10 ]; do
     echo The counter is $COUNTER
     let COUNTER=COUNTER+1 
 done

Until sample

   #!/bin/bash 
         COUNTER=20
         until [  $COUNTER -lt 10 ]; do
             echo COUNTER $COUNTER
             let COUNTER-=1
         done

Arrays in Bash

#!/bin/bash

products=( A B C D E F G )
echo "${products[0]}"
echo "${products[1]}"
echo "${products[2]}"
echo "${products[3]}"
echo "${products[4]}"
echo "${products[5]}"
echo "${products[6]}"

Looping over Arrays

#!/bin/bash

members=(rahika sejal Jake Scott Philis)
for m in "${members[@]}"
do
    echo "$m is a registered member"
done

I hope you get an idea about bash script example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment