bash HOSTNAME 在环境中设置但对 gmake 不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6353065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HOSTNAME set in environment but not visible to gmake
提问by jcomeau_ictx
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.
多年来,这一直困扰着我。谁能解释为什么$(HOSTNAME)不扩展到环境值 HOSTNAME?谷歌搜索“make”、“主机名”、“gmake”、“未设置”等的各种组合对我来说没有成果。
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
回答by cxreg
This is the difference between a shell variable and an environment variable. Try running "export HOSTNAME" before your test and see the difference.
这是 shell 变量和环境变量之间的区别。在测试之前尝试运行“export HOSTNAME”并查看差异。
Also, compare the difference in output between "set" and "env".
另外,比较“set”和“env”之间的输出差异。
回答by lunixbochs
When you start a subprocess in a shell (perhaps by running the makecommand), 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.
当您在 shell 中启动子进程时(可能通过运行make命令),它继承了 shell 的原始环境变量,以及从 shell 内部“导出”的所有变量。如果 shell 的原始环境没有 $HOSTNAME 并且 $HOSTNAME 没有被导出,它就不会在衍生进程的环境中。
Run exportto 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 HOSTNAMEon the command line, or in your .bashrc/profile.
运行export以查看导出的变量列表。在我的系统上,默认情况下 $HOSTNAME 不在导出中。如果您希望 HOSTNAME 在生成的进程中可用,您可以export HOSTNAME在命令行上或在您的 .bashrc/profile 中。
回答by Dimitrios
If you want to export hostname you can use:
如果要导出主机名,可以使用:
export SERVERIP=$(hostname --ip-address)
echo $SERVERIP

