The Wayback Machine - https://web.archive.org/web/20240118180135/https://www.geeksforgeeks.org/array-basics-shell-scripting-set-2-using-loops/
Open In App
Related Articles

Array Basics Shell Scripting | Set 2 (Using Loops)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

It is recommended to go through Array Basics Shell Scripting | Set-1 Introduction Suppose you want to repeat a particular task so many times then it is a better to use loops. Mostly all languages provides the concept of loops. In Bourne Shell there are two types of loops i.e for loop and while loop. To Print the Static Array in Bash 1. By Using while-loop ${#arr[@]} is used to find the size of Array. 

bash




# !/bin/bash
# To declare static Array
arr=(1 12 31 4 5)
i=0
 
# Loop upto size of array
# starting from index, i=0
while [ $i -lt ${#arr[@]} ]
do
    # To print index, ith
    # element
    echo ${arr[$i]}
     
    # Increment the i = i + 1
    i=`expr $i + 1`
done


Output:

1
2
3
4
5

Time Complexity: O(n)
Auxiliary Space: O(1)

2. By Using for-loop 

bash




# !/bin/bash
# To declare static Array
arr=(1 2 3 4 5)
 
# loops iterate through a
# set of values until the
# list (arr) is exhausted
for i in "${arr[@]}"
do
    # access each element
    # as $i
    echo $i
done


Output:

1
2
3
4
5

Time Complexity: O(n)
Auxiliary Space: O(1)

To Read the array elements at run time and then Print the Array. 1. Using While-loop 

bash




# !/bin/bash
# To input array at run
# time by using while-loop
 
# echo -n is used to print
# message without new line
echo -n "Enter the Total numbers :"
read n
echo "Enter numbers :"
i=0
 
# Read upto the size of
# given array starting from
# index, i=0
while [ $i -lt $n ]
do
    # To input from user
    read a[$i]
 
    # Increment the i = i + 1
    i=`expr $i + 1`
done
 
# To print array values
# starting from index, i=0
echo "Output :"
i=0
 
while [ $i -lt $n ]
do
    echo ${a[$i]}
 
    # To increment index
    # by 1, i=i+1
    i=`expr $i + 1`
done


Output:

Enter the Total numbers :3
Enter numbers :
1
3
5
Output :
1
3
5

Time Complexity: O(n)
Auxiliary Space: O(1)

2. Using for-loop 

bash




# !/bin/bash
# To input array at run
# time by using for-loop
 
echo -n "Enter the Total numbers :"
read n
echo "Enter numbers:"
i=0
 
# Read upto the size of
# given array starting
# from index, i=0
while [ $i -lt $n ]
do
    # To input from user
    read a[$i]
 
    # To increment index
    # by 1, i=i+1
    i=`expr $i + 1`
done
 
# Print the array starting
# from index, i=0
echo "Output :"
 
for i in "${a[@]}"
do
    # access each element as $i
    echo $i
done


Output:

Enter the Total numbers :3
Enter numbers :
1
3
5
Output :
1
3
5

Time Complexity: O(n)
Auxiliary Space: O(1)


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads