Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment the elements of an array by 1 (1 thru 1000)

I ma still a ROOKIE when it comes to shell script. Long story short I am trying the increment the values of the array by one for every iteration. Here is my code

cmd=(1 2 3 4 5 6 7 8 ................)  // How can I pass numbers 1 to 1000 with out having to type manually.
${cmd[@]}

for (( i = 0 ; i < ${#cmd[@]} ; i++ )) do
echo ${cmd[$i]}"

done  

One approach would be cmd=() and then inside the loop we add the line "let cmd[i]++" , but it didnt work for me. Thanks in advance

like image 245
z atef Avatar asked Dec 07 '22 04:12

z atef


2 Answers

Try the seq command

cmd=( $(seq 1 1000) )
like image 196
eduffy Avatar answered Jan 02 '23 18:01

eduffy


If you are running bash you may take advantage of its features.

Try:

cmd=({1..1000})
like image 36
alvits Avatar answered Jan 02 '23 18:01

alvits