2

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 ?

1
  • 1
    One has to wonder what your use case is for iterating over that range in Bash. Feb 1, 2014 at 20:19

2 Answers 2

2

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
2
  • In the meantime, while you wait for the bug to be fixed, could you simply break your loop in half? for i in {0..2147483647}; do echo $i; done; for i in {2147483648..4294967295}; do echo $i; done so you don't run into the SIGSEGV that @devnull describes?
    – OnlineCop
    Feb 1, 2014 at 18:31
  • 1
    The C-style for loop is much better in this case. It completely avoids the problem. It's sort of analogous to using xrange instead of range in Python. Feb 1, 2014 at 20:15
0

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

2
  • it would take 2-3 Terabytes of memory
    – mjz19910
    Feb 2, 2014 at 20:05
  • I counted 47GB to store all the text output. Anyway, the OP had to put something in the loop otherwise it would be a syntax error. It's normally assumed that the OP has meaningful complex logic that they are running inside their loop in their actual code.
    – Jack G
    Feb 5, 2022 at 0:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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