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
san45
  • 105

1 Answers1

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
choroba
  • 47,233