I'm trying to read a structured file into an associative array in Bash. The file stores in each line a person name and a person address. For example:
person1|address1
person2|address2
...
personN|addressN
I am using the script below.
#!/bin/bash
declare -A address
while read line
do
name=`echo $line | cut -d '|' -f 1`
add=`echo $line | cut -d '|' -f 2`
address[$name]=$add
echo "$name - ${address[$name]}"
done < adresses.txt
for name in ${!address[*]}
do
echo "$name - ${address[$name]}"
done
The script work properly. However, in the FOR loop, i'm having some problems when the person name has spaces (For example "John Nobody"). How can I fix this?