Bash – KSH/BASH Maximum size of an Array

bashkshshell-script

What is the maximum size of array for ksh and bash scripting?

Example: Suppose I have an array of 10 elements. What will be the maximum number of string count that a particular index of an array can hold? What will be the maximum size of an array for the same?

I am new to Unix. I imagine this is a common question but I didn't manage to find an answer so I decided to ask here.

Best Answer

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.)

Related Question