how to create an array

Hi, is there a faster way to create an array containing list of numbers? something like the ones below.

array=(01 02 03 04 05 06 07 08 09 10 11 ... 365)

thanks much.

arr=$(seq 1 365)

or, if you need zeroes prepended:

arr=$(seq -f "%03.0f" 1 365)

The '1' is not really needed in this case, but illustrates that you can start the sequence at an arbitrary integer.

Hi mirni, thanks for the reply. had a question though, i need to use the contents of the array to name an output file using this syntax. will it possible to do such.

arr=(01 02 03 04); n=0
for i in $list; do
    command > "1999${arr[n++]}.grd"
done
desired output: 199901, 199902 199903 199904
arr=(01 02 03 04); n=0 
for i in $list; do
     command > "1999${arr[$((n++))]}.grd" 
done

See man bash and search for "arithmetic expansion".

an array seems useless in this case

for (( x=1; x<=365; x++)); do printf -v maVar '%.2d' $x; command >1999$maVar; done

btw, arithmetic expansion is done inside array elements without the need of double parenthesis, ${array[n++]} should work.

1 Like