KSH/BASH Maximum size of an Array

i=0
while true; do
    a[$i]=foo
    i=$((i+1))
    printf "\r%d " $i
done

This simple script shows on my systems (Gnu/Linux and Solaris):

  • ksh88 limits the size to 2^12-1 (4095). (subscript out of range ). Some older releases like the one on HP-UX limit the size to 1023.

  • ksh93 limits the size of a array to 2^22-1 (4194303), your mileage may vary.

  • bash doesn't look to impose any hard-coded limit outside the one dictated by the underlying memory resources available. For example bash uses 1.3 GB of virtual memory for an array size of 18074340.

Note: I gave up with mksh which was too slow executing the loop (more than one hundred times slower than zsh, ksh93 and bash.)


There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions (see Shell Arithmetic)) and are zero-based; associative arrays use arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

http://www.gnu.org/software/bash/manual/html_node/Arrays.html


It depends on the implementation. In ksh there are documented "implementation defined limits" for indexed arrays. For ksh88 there are systems existing with a limit of 1023, for ksh93 the minimum limit required by an implementation is 4095. So you cannot count on having more available than that! (If you are only implementing for a specific system you can test your system limits as proposed in another answer here.)