Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill an array with a loop in BASH

Tags:

People also ask

How do I loop through an array in bash?

There are two ways to iterate over items of array using For loop. The first way is to use the syntax of For loop where the For loop iterates for each element in the array. The second way is to use the For loop that iterates from index=0 to index=array length and access the array element using index in each iteration.

How do I add elements to an array in bash?

Using shorthand operators is the simplest way to append an element at the end of an array. In the following script, an array with 6 elements is declared. Next '+=' shorthand operator is used to insert a new element at the end of the array. 'for' loop is used here to iterate the array and print the array elements.

Can you do a loop in bash?

There is no do-while loop in bash. To execute a command first then run the loop, you must either execute the command once before the loop or use an infinite loop with a break condition.

Can bash function return array?

However, BASH lacks the ability to return arrays from functions.


I would like to fill an array automatically in bash like this one:

200 205 210 215 220 225 ... 4800

I tried with for like this:

for i in $(seq 200 5 4800);do
    array[$i-200]=$i;
done

Can you please help me?