0

So I have a list of a set of numbers going from 1 to 100, I intended to iterate over it to obtain for every number every single number below it. Like 3 = 1 2 3, or 10 = 1 2 3 4 5 6 7 8 9 10. However the function I'm trying to use isn't working.

for i in ${BigList[@]:0:100}
do 
for j in $(echo {1..$i})
do 
echo $j;sleep 1 
done 
done

The following code is outputing

{1..1}
{1..2}
{1..3}
{1..4}

When in fact I was trying to get it to output this: 1 --> 1 2 --> 1 2 3 --> 1 2 3 4. Something like this. The goal is to get all the numbers until that particular one

Ysgramor 500
  • 13
  • 1
  • 4
  • 1
    Does this answer your question? [How do I iterate over a range of numbers defined by variables in Bash?](https://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash) – Ruud Helderman Jul 17 '22 at 15:54

1 Answers1

1

$(echo {1..$i}) suffers from the same problem as {1..$i} alone: brace expansion precedes parameter expansion; command substitution does nothing to address that. You could use eval:

for j in eval {1..$i}

but it's better to avoid eval unless absolutely necessary, which it is not here:

for ((j=1; j<=$i; j++))
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Because of math mode you can write `for ((j=1; j<=i; j++))` instead of `for ((j=1; j<=$i; j++))`. – Wiimm Jul 18 '22 at 17:58