While dealing with getting keys of arrays, I found out that associative array keys where not registered in the same order as declared:

#!/usr/bin/env bash
# Declare and populate an associative array a
unset a
declare -A a=(
typeset -p a # show actual declaration order that differs from real one
# Show how the chaotic order affect iteration of the array
for v in "${a[@]}"; do
  echo "$v"

declare -A a=([two]="second" [three]="third" [four]="last" [one]="first"

This behavior looks just wrong and it is just same if you build the array incrementally:

unset a; declare -A a; a=(["one"]="first"); a+=(["two"]="second"); a+=(["three"]="third"); a+=(["four"]="last"); typeset -p a

Is there a way to control the order of entries in an associative array?

What rules applies to the order of entries?

Léa Gris

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to