malat
malat

Reputation: 12502

bash: for loop with unsigned integer

I am trying to iterate over all unsigned int (32bits). I thought I could simply do:

#!/bin/bash

for i in {0..4294967295}; do
  echo $i
done

However with bash 4.1, all it prints out is:

$ ./loop.sh
0
-1

I certainly cannot use $seq() construction, so how does one do a for loop in bash over all uint32 ?

Upvotes: 2

Views: 1232

Answers (2)

mjz19910
mjz19910

Reputation: 242

Note that this many values would use up so much memory that it could never be done on any computer because it would take tremendous amounts of memory

Upvotes: 0

devnull
devnull

Reputation: 123608

Using bash 4.2, I get a SIGSEGV with your example. It seems that the problem is due to the fact you introduce 4294967295 + 1 number of arguments (essentially expanded by {0..4294967295}) in your for loop which causes it to choke.

Nevertheless, both the SIGSEGV that I observe and the incorrect behavior that you observe warrant that the issue be reported.

The home page says that the main discussion list is <[email protected]>.


You could use a modified for loop:

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

Upvotes: 3

Related Questions