Loop through Elements of Array
To loop through elements of array in Bash, use the expression ${arr[@]}
to get all the elements and use For loop to loop through each one of these elements.
Example
In the following script, we take an array arr
with three elements and iterate over the elements of this array using For loop.
Example.sh
</>
Copy
arr=("apple" "banana" "cherry")
for element in "${arr[@]}";
do
echo $element
done
Output
apple
banana
cherry
Conclusion
In this Bash Tutorial, we have learnt how to loop through the elements of an array in Bash shell.