To illustrate the issue, I created a makefile which first defines a list of cars and then for each car, a list of colours:
CARS = Ford Ferrari Mercedes Toyota
Ford_COLOURS = black blue red white
Ferrari_COLOURS = red
Mercedes_COLOURS = black blue silver
Toyota_COLOURS = blue red white
Then, I'd like to loop through all colours of every car in one of my make targets:
.PHONY: list
list:
for car in ${CARS} ; do \
car_colours=$${car}_COLOURS ; \
echo $${car_colours} = "$${!car_colours}" ; \
eval colours="$${!car_colours}" ; \
for colour in $${colours} ; do \
echo $${colour} ; \
done ; \
done
As you can see I tried to synthesise the ideas from this question about generating dynamic variable names in bash and some answers to this question about how to evaluate (expand) dynamic names in bash.
Unfortunately I get the following output:
Ford_COLOURS =
Ferrari_COLOURS =
Mercedes_COLOURS =
Toyota_COLOURS =
while I was hoping to get something like this:
Ford_COLOURS = black blue red white
black
blue
red
white
Ferrari_COLOURS = red
red
Mercedes_COLOURS = black blue silver
black
blue
silver
Toyota_COLOURS = blue red white
blue
red
white
How would I fix that?