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

1

1 Answer 1

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++))
Sign up to request clarification or add additional context in comments.

1 Comment

Because of math mode you can write for ((j=1; j<=i; j++)) instead of for ((j=1; j<=$i; j++)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.