HOSTNAME 在环境中设置但对 gmake 不可见
这多年来断断续续地困扰着我。谁能解释为什么 $(HOSTNAME)
不扩展到环境值 HOSTNAME?谷歌搜索“make”、“hostname”、“gmake”、“not set”等的各种组合对我来说并没有什么成果。
jcomeau@intrepid:/tmp$ set | egrep 'HOSTNAME|USER'
HOSTNAME=intrepid
USER=jcomeau
jcomeau@intrepid:/tmp$ cat Makefile
%.test:
set | grep $*
%.env:
echo $($*)
jcomeau@intrepid:/tmp$ make HOSTNAME.test
set | grep HOSTNAME
BASH_EXECUTION_STRING='set | grep HOSTNAME'
HOSTNAME=intrepid
jcomeau@intrepid:/tmp$ make HOSTNAME.env
echo
jcomeau@intrepid:/tmp$ make USER.test
set | grep USER
BASH_EXECUTION_STRING='set | grep USER'
USER=jcomeau
jcomeau@intrepid:/tmp$ make USER.env
echo jcomeau
jcomeau
This has plagued me on and off for years. Can anyone explain why $(HOSTNAME)
doesn't expand to the environment value HOSTNAME? Googling various combinations of "make", "hostname", "gmake", "not set" and so forth hasn't been fruitful for me.
jcomeau@intrepid:/tmp$ set | egrep 'HOSTNAME|USER'
HOSTNAME=intrepid
USER=jcomeau
jcomeau@intrepid:/tmp$ cat Makefile
%.test:
set | grep $*
%.env:
echo $($*)
jcomeau@intrepid:/tmp$ make HOSTNAME.test
set | grep HOSTNAME
BASH_EXECUTION_STRING='set | grep HOSTNAME'
HOSTNAME=intrepid
jcomeau@intrepid:/tmp$ make HOSTNAME.env
echo
jcomeau@intrepid:/tmp$ make USER.test
set | grep USER
BASH_EXECUTION_STRING='set | grep USER'
USER=jcomeau
jcomeau@intrepid:/tmp$ make USER.env
echo jcomeau
jcomeau
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是 shell 变量和环境变量之间的区别。尝试在测试之前运行“export HOSTNAME”并查看差异。
另外,比较“set”和“env”之间的输出差异。
This is the difference between a shell variable and an environment variable. Try running "export HOSTNAME" before your test and see the difference.
Also, compare the difference in output between "set" and "env".
当您在 shell 中启动子进程时(可能通过运行 make 命令),它会继承 shell 的原始环境变量,以及 shell 内部的所有“导出”变量。如果 shell 的原始环境没有 $HOSTNAME 并且 $HOSTNAME 未导出,则它不会位于生成进程的环境中。
运行
export
以查看导出变量的列表。在我的系统上,默认情况下 $HOSTNAME 不在导出中。如果您希望 HOSTNAME 在生成的进程中可用,您可以在命令行或 .bashrc/profile 中导出 HOSTNAME
。When you start a subprocess in a shell (perhaps by running the
make
command), it inherits the shell's original environment variables, plus all "exported" variables from inside the shell. If the shell's original environment didn't have $HOSTNAME and $HOSTNAME wasn't exported, it won't be in the environment of a spawned process.Run
export
to see a list of exported variables. On my system, $HOSTNAME isn't in the exports by default. If you want HOSTNAME to be available in a spawned process, you canexport HOSTNAME
on the command line, or in your .bashrc/profile.如果你想导出主机名,你可以使用:
If you want to export hostname you can use: