HOSTNAME 在环境中设置但对 gmake 不可见

发布于 2024-11-15 20:48:40 字数 658 浏览 3 评论 0原文

这多年来断断续续地困扰着我。谁能解释为什么 $(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-11-22 20:48:40

这就是 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".

一梦浮鱼 2024-11-22 20:48:40

当您在 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 can export HOSTNAME on the command line, or in your .bashrc/profile.

对不⑦ 2024-11-22 20:48:40

如果你想导出主机名,你可以使用:

export SERVERIP=$(hostname --ip-address)
echo $SERVERIP

If you want to export hostname you can use:

export SERVERIP=$(hostname --ip-address)
echo $SERVERIP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文