On Fri, Aug 09, 2013 at 10:47:21PM -0600, Bob Proulx wrote: > John Vincent wrote: > > echo {1..20000000} > > it runs, but afterwards whenever I enter another command I get the error: > > -bash: fork: Cannot allocate memory > > You need to allocate more virtual memory space to your machine. You > are asking it to do more than it can do with the memory it has > available.
Brace expansion causes bash to generate the full list of expanded words in memory all at once. This means bash has to allocate enough memory to hold them, all at once. 20 million strings of varying length from 1 to 8 bytes, plus NUL terminators, plus all the overhead of the memory allocation system... is a lot of wasted memory just to count integers. If you use a C-style for loop instead, then bash doesn't have to generate and store the entire list of strings all at once: for ((i=1; i<=20000000; i++)); do printf "%d " "$i" done echo (That's not 100% equivalent because the for loop generates one extra space right before the final newline.)