Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

IINM my system is failing when bashing

for i in {0..10000000}; #   Seven zeroes.
do
    false;
done #   `bash` exited and its `tmux` pane/window was closed.

or

for i in $(seq 0 10000000); #   Seven zeroes.
do
    false;
done #   `bash` exited and its `tmux` pane/window was closed.

but not when

for i in {0..1000000}; #   Six zeroes.
do
    false;
done #   Finished correctly.

Can you please briefly explain the internals of this behavior and prompt a workaround for getting the task done?

share|improve this question
1  
For you my all loved XY friends, a big file with CRLFs only is what I am actually trying to get. –  galegosimpatico Aug 27 at 12:28
    
Testing nl with lines alot, tabs and XY pow are what I got. –  galegosimpatico Aug 27 at 12:48

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
1  
there's also the old /dev/zero/ thing... like tr \\0 \\r </dev/zero | tee - - - | dd or head or whatever... It might be better to mount a tmpfs of that size and fill it then cp to a file. Maybe not... –  mikeserv Aug 27 at 12:35
2  
@mikeserv, yes Pourquoi faire simple quand on peut faire compliqué ? like they say in France. –  Stéphane Chazelas Aug 27 at 12:38
    
dang man. you really are a funny guy sometimes. now I have to translate that... ahh. the double entendre. that's probably French too. and, yeah, I dunno why I do. but I do. –  mikeserv Aug 27 at 12:39
    
Telling you to work a pearl for it. –  galegosimpatico Aug 27 at 12:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.