`seq` and bash brace expansion failing
for i in {0..1000000}
and for i in $(seq 1000000)
both build up a big list and then loop over it. That's inefficient and uses a lot of memory.
Use:
for ((i = 0; i<= 1000000; i++))
instead. Or POSIXly:
i=0; while [ "$i" -le 1000000 ]; do
...
i=$(($i + 1))
done
Or:
seq 1000000 | xargs...
To get a file full of CRLFs:
yes $'\r' | head -n 1000000 > file
Generally, loops should be avoided when possible in shells.
@Stéphane Chazelas gave a great answer up there.
Mine is not an answer. I just it got curious about the efficiency of the different solutions, so I thought I should share my results...
~# i=0 ; time while [ "$i" -le 1000000 ]; do ((i++)) ;done
real 0m21.753s
~# time for ((i = 0; i<= 1000000; i++)) ;do : ;done
real 0m15.791s
~# time for i in {0..1000000} ;do : ;done
real 0m8.897s
~# time for i in $(seq 0 1000000) ;do : ;done
real 0m7.760s
:~# time seq 1000000 | xargs true
real 0m0.938s
~# time yes $'\r' | head -n 1000000 >/dev/null
real 0m0.048s
The yes
answer is the clear winner. :-)
Besides, it actually does the work that the OP needed.