Bash For Loop Array: Iterate Through Array Values

www.‮‬lautturi.com
Bash For Loop Array: Iterate Through Array Values

To iterate through the values of an array in Bash, you can use a for loop. Here is an example of how to use a for loop to iterate through the values of an array:

my_array=(apple banana cherry)

for value in ${my_array[@]}; do
  echo $value
done

In this example, the for loop will iterate through each value in the my_array array. The ${my_array[@]} syntax is used to expand the array and provide a list of its values to the for loop. Inside the loop, the $value variable is used to access each array value, and the echo command is used to print it to the console.

This will output each value of the array on a separate line, like this:

apple
banana
cherry

You can use this approach to perform any operations on each array value inside the for loop, such as modifying the values, or using them as arguments for other commands. Consult the Bash documentation for more information about using for loops and working with arrays in Bash.

Created Time:2017-10-16 14:38:38  Author:lautturi