0

I would like to modify the value of louis to green in this bash3 map:

ARRAY=( "nicolas:red"
    "louis:blue" )

I tried to overwrite with ARRAY+=("louis:green") but it does not work: I end up with a 3-elements array with duplicate key.

Note: I can't use bash4's modern maps so I have to use bash3 associative arrays.

Community
  • 1
  • 1
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

1 Answers1

1

This is just a regular (integer-)indexed array. You'll have to iterate over it to find the correct index to replace. (This will also work for adding a new element, as the index will be past the end of the existing array.)

for ((i=0; i<${#ARRAY[*]}; i++)); do
    [[ ${ARRAY[i]} = louis:* ]] && break
done
ARRAY[i]=louis:green
ephemient
  • 198,619
  • 38
  • 280
  • 391