Reputation: 43
im having issues with $(foreach..) and echo functions. im trying to iterate over a variable and append it into a file.
here is my code:
vars = a b c d
all:
@$(foreach var, $(vars), echo $(var) >> variables.txt)
the generated file looks like
a echo b echo c echo d
i have no idea why it prints also the word "echo" and why it is on the same line.
Im working on Windows, on linux the results are as expected.
what am i missing here?
thanks.
Upvotes: 0
Views: 59
Reputation: 99172
Funny thing about output redirection. You can put it at the end of a command:
echo a b c >> filename
Or in the middle of a command:
echo a b >> filename c
Or in multiple places:
echo a >> filename b >> filename c >> filename
and it does exactly the same thing.
In your makefile, this:
$(foreach var, $(vars), echo $(var) >> variables.txt)
is a Make iteration. (The shell has no such command as foreach
), and it expands into this:
echo a >> variables.txt echo b >> variables.txt echo c >> variables.txt echo d >> variables.txt
which, as we've seen, is equivalent to:
echo a echo b echo c echo d >> variables.txt
So the shell dutifully echoes a echo b echo c echo d
and redirects it to variables.txt
.
EDIT: The simplest way to get the effect you want is to add a semicolon:
vars = a b c d
all:
@$(foreach var, $(vars), echo $(var) >> variables.txt;)
Upvotes: 1