Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I have the following to work on:

for i in {2..10}

do

    echo "output: $i"

done

It gives the output: 2, output: 3, so on.

Then, tried to run the following:

max=10

for i in {2..$max}

do

    echo "$i"

done

produces with the following:

output: {2..10}

How can I get this compiler to realize it had to be as $max and the other end of this array, not part of the string?

1 Answer

0 votes
by (36.8k points)

Brace expansion {x..y}, is usually used before any other expansion, so you cannot use this for variable-length sequences.

Instead, use the seq 2 $max method.

for example it would be:

max=10

for i in `seq 2 $max`

do

    echo "$i"

done

To know about Linux join the Linux training

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 10, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...