Variables in bash are loosely typed; however, declaring provides a type-like behavior. One of the supported types in bash is an associative array (-A).
To declare an associative array:
1
declare -A scores
To declare and instantiate in a single line:
1
declare -A scores=(["peter"]=95["sally"]=98)
Add key/value pairs
Adding key/value pairs to an existing associate array:
1
2
3
4
5
6
scores["tom"]=85# using variablesname="brandon"score=50scores[$name]=$score
Retrieve key/value pairs
Retrieving key/value pairs from an existing associative array:
1
2
name="peter"echo${scores[$name]}# 95
Update an existing key/value pair
Updating an existing key/value pair in an existing associative array: