Bash: How to iterate over a range of numbers defined by variable


Method 1:

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

Method 2:

let END=5 i=1
while ((i<=END)); do echo $i; let i++; done

Method 3:

END=5
for ((i=1;i<=END;i++)); do echo $i; done

Method 4:

END=5
for i in $(eval echo "{1..$END}"); do echo $i; done

Leave a Reply