given a config file as follows :-
[a]
b=C
d=E;rm t1
[b]
g=h
the following one-liner will parse and hold the values :-
CFG=path-to-file; for ini in `awk '/^\[/' $CFG`;do unset ARHG;declare -A ARHG;while read A B;do ARHG[$A]=$B;echo "in section $ini, $A is equal to" ${ARHG["$A"]};done < <(awk -F'=' '/\[/ {x=0} x==1 && $0~/=/ && NF==2 {print $1, $2} $0==INI {x=1}' INI="$ini" $CFG);declare -p ARHG;echo;done;printf "end loop\n\n";declare -p ARHG
Now, let's break that down
CFG=path-to-file;
for ini in `awk '/^\[/' $CFG` # finds the SECTIONS (aka "ini")
do
unset ARHG # resets ARHG
declare -A ARHG # declares an associative array
while read A B
do
ARHG[$A]=$B
echo "in section $ini, $A is equal to" ${ARHG["$A"]}
done < <(awk -F'=' '/\[/ {x=0} x==1 && $0~/=/ && NF==2 {print $1, $2} $0==INI {x=1}' INI="$ini" $CFG)
# the awk splits the file into sections,
# and returns pairs of values separated by "="
declare -p ARHG # displays the current contents of ARHG
echo
done
printf "end loop\n\n"
declare -p ARHG
This allows us to save values, without using eval or backtick.
To be "really clean", we could remove [:space:] at the start and end of line, ignore "^#" lines, and remove spaces around the "equals" sign.