Free Hosting Online for WorkStations

< Previous | Contents | Next >

Associative Arrays

Recent versions of bash now support associative arrays. Associative arrays use strings

Associative Arrays


rather than integers as array indexes. This capability allow interesting new approaches to managing data. For example, we can create an array called “colors” and use color names as indexes:



declare -A colors colors["red"]="#ff0000" colors["green"]="#00ff00" colors["blue"]="#0000ff"

declare -A colors colors["red"]="#ff0000" colors["green"]="#00ff00" colors["blue"]="#0000ff"


Unlike integer indexed arrays, which are created by merely referencing them, associative arrays must be created with the declare command using the new -A option. Associa- tive array elements are accessed in much the same way as integer indexed arrays:



echo ${colors["blue"]}

echo ${colors["blue"]}


In the next chapter, we will look at a script that makes good use of associative arrays to produce an interesting report.


Top OS Cloud Computing at OnWorks: