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
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
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