是否可以在 Makefile 中设置环境变量 - 之后使用
我试图在 Makefile 中设置一个环境变量,以便它可以在 sam shell 中作为 make
运行的另一个程序中使用,但在 make
运行之后。
更新:根据已接受的答案和评论,这是不可能的。
步骤:
- 运行
make test
设置环境:export TEST_ENV_ONE=OneString
- 运行另一个程序,可以读取
TEST_ENV_ONE
尝试过这个:
不起作用:
test:
export TEST_ENV_ONE=OneString
$(shell export TEST_ENV_TWO=TwoString)
之后这是空的:
echo $TEST_ENV_ONE
echo $TEST_ENV_TWO
I am trying to set an Environment variable in a Makefile, so it can be used in another program running in the sam shell as make
, but after make
has run.
Update: This is not possible according to accepted answer with comments.
Steps:
- run
make test
setting env:export TEST_ENV_ONE=OneString
- run another program, that can read
TEST_ENV_ONE
Tried this:
Not working:
test:
export TEST_ENV_ONE=OneString
$(shell export TEST_ENV_TWO=TwoString)
Afterwards this is empty:
echo $TEST_ENV_ONE
echo $TEST_ENV_TWO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上面的
export TEST_ENV_ONE=OneString
正在专用 shell 中运行。后续命令在其他 shell 实例中运行。因此,它们不会继承环境变量TEST_ENV_ONE
。您可以使用顶级(即不在目标配方中)< makefile 中的 code>export 指令:
这样,变量
env_var
就会导出到将执行配方的 shell。如果您使用上面的 makefile 运行
make
:从输出中可以看到,运行
echo env_var: $env_var
的 shell 具有变量env_var
在其环境中。Your
export TEST_ENV_ONE=OneString
above is running in a dedicated shell. The subsequent commands run in other shell instances. Therefore, they don't inherit the environment variableTEST_ENV_ONE
.You could use a top-level (i.e., not in a target's recipe)
export
directive in the makefile:This way, the variable
env_var
is exported to the shells that will execute the recipes.If you run
make
with the makefile above:As you can see from the output, the shell that run
echo env_var: $env_var
had the variableenv_var
in its environment.如果您希望将环境变量导出到调用 make 的 shell,事情会有点困难,因为正如 ネroku 所解释的,您不能直接将环境变量从子进程 (
make
) 导出到它的父进程(调用make
的 shell)。但是,如果您接受像这样调用 make:那么确实是可能的:只需 echo
export VARIABLE1=VALUE1;导出变量2=值2; ...
在你的食谱中。警告:您还必须保证 make 在标准输入上不会回显任何其他内容。但您可以改用标准错误。示例:请注意,make 或多或少将环境变量视为make 变量。来自 GNU make 手册:
因此,除非变量的值是 make 本身复杂计算的结果,否则获得相同结果的更自然的方法是在父 shell 中定义环境变量并使用它与 Makefile 中一样:
If you want the environment variables to be exported to the shell from which you invoked make things are a bit difficult because, as explained by ネロク, you cannot directly export environment variables from a child process (
make
) to its parent process (the shell from which you invokemake
). But if you accept to invoke make like this:then it is indeed possible: just echo
export VARIABLE1=VALUE1; export VARIABLE2=VALUE2; ...
in your recipe. Warning: you will also have to guarantee that nothing else gets echoed by make on the standard input. But you can use the standard error, instead. Example:Note that make more or less considers environment variables as make variables. From GNU make manual:
So, unless the value of your variable is the result of complex computations by make itself, a much more natural way to obtain the same result would be to define the environment variable in the parent shell and to use it as is in the Makefile: