How to set environment variables in makefile recipe?

Here is a simplified makefile:

all: @for (( i = 0; i < 5; ++i )); do \ var="$$var $$i"; \ echo $$var; \ done @echo $$var 

I suppose the value of "var" is "0 1 2 3 4", but the output is:

 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 <--- NOTHING!!! 

As you can see, the last echo is NOTHING. What's wrong?

+6
source share
1 answer

From here :

When it is time to execute recipes to update the target, they are executed by calling a new subshell for each line of the recipe ...

Note: means that setting shell variables and calling shell commands, such as cd , which set the local context for each process, will not affect the following lines in the recipe. If you want to use cd to influence the next statement, put both statements on the same line of the recipe. Then make will call one shell to run the entire line, and the shell will execute the statements in sequence.

Try the following:

 all: @for (( i = 0; i < 5; ++i )); do \ var="$$var $$i"; \ echo $$var; \ done; \ echo $$var 
+10
source

Source: https://habr.com/ru/post/913239/


All Articles