[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: unsigned int for loop in bash

From: Greg Wooledge
Subject: Re: unsigned int for loop in bash
Date: Mon, 3 Feb 2014 08:22:52 -0500
User-agent: Mutt/

On Sat, Feb 01, 2014 at 05:36:17PM +0100, Mathieu Malaterre wrote:
> #!/bin/bash
> for i in {0..4294967295}; do
>   echo $i
> done

Others said not to do that.  They are correct, but they didn't tell you
what to do instead.

for ((i=0; i<=4294967295; i++)); do
  echo $i

The brace expansion form actually expands to the full list of strings,
all at once, which means they are all stored in memory at once.  Most
computers don't have that much memory (something like 40 GB to hold
your 4 billion strings, of variable length from 2 to 11 bytes each).
The C-style for loop doesn't generate them all at once, so it only uses
a few bytes of memory.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]