Associative Arrays in Bash v4

One of the new features in Bash v4 are the associative arrays using strings as index instead numbers
here a small example of use


#!/bin/bash

declare -A myArray #declare array
myArray[cat]=gato #attach a value into index cat
myArray[dog]=can #attach a value into index dog

creature="fish" #declare a variable

myArray[$creature]=peixe #attach a value into index fish

#get all array values
for key in "${!myArray[@]}"
 do
 echo "index value=$key content value= ${myArray[$key]}"
 done

Leave a Reply

Your email address will not be published. Required fields are marked *