0

Can someone explain why the following code

for i in {1..5};do
echo "hello"
done

prints

hello
hello
hello
hello
hello

but the following

num=5

for i in {1..$num};do
echo "hello"
done

prints

hello
0

1 Answer 1

1

That's because brace expansion happens before variable expansion.

You can use seq instead:

num=5
for i in $(seq 1 $num) ; do
    echo hello
done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.