Given a file of the following format:
whitelist_a=["10.2.3.4", "10.6.7.8"]
whitelist_b=["192.2.3.4", "192.6.7.8"]
How can I parse it and use eval
to create local variables for each key-value pair that I can reference later in my script? This is what I've tried so far:
WHITELISTS=./whitelists.txt
IFS="="
while read -r name value
do
eval "$name=${value}"
done < $WHITELISTS
If I temporarily change the eval
to echo
then from the output I can see the lines being parsed as expected:
whitelist_a=["10.2.3.4", "10.6.7.8"]
whitelist_b=["192.2.3.4", "192.6.7.8"]
However, switching back to eval
I get 10.6.7.8]: command not found
, which I'm presuming is because the double quotes haven't been escaped. What do I need to change to make this work?