How to Write Bash For Loop Array

In this tutorial we will show you how to write a bash for loop array. This command let’s your make a string and form a loop that is repeated in the output. Loops and arrays are easy to create in Linux.
To understand how to create a loop array, follow the tutorial steps given below.



Step 1 – Declare a variable and create a string
First of all, we will declare an array variable over here. Now we will create a string of element 1, 2 and 3 over here, and we will assign it in the array variable that we have already declared before.
declare –a arr=(element1 element2 element3)
Assign variable and make a string

Step 2 – Loop the array
Next, we will loop through the above array and display all the elements inside the array.
For that, we will use the “at the rate” sign over here.
for i in ${arr[@]}
Make a loop of the array

Step 3 – View the output on the screen
With that done, the output of the bash loop array will be displayed on the screen.
And there you have it, a basic for command for loops and arrays written in bash.
do
echo $i
done
View the loop array