How to use variables in a bash for loop [duplicate]

One way is using eval:

for i in $( eval echo {0..$length} )
do
       echo "do something right $i"
done

Note what happens when you set length=;ls or length=; rm * (don’t try the latter though).

safely, using seq:

for i in $( seq 0 $length )
do
       echo "do something right $i"
done

or you can use the c-style for loop, which is also safe:

for (( i = 0; i <= $length; i++ )) 
do 
       echo "do something right $i"
done

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)