bash System.getenv 没有获得 ~/.bash_profile 中定义的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12304647/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:13:27  来源:igfitidea点击:

System.getenv doesn't get variables defined in ~/.bash_profile

javalinuxbash

提问by Patrick Lorio

Here is a line in file ~/.bash_profile

这是文件 ~/.bash_profile 中的一行

export MESSAGE="Hello World"

export MESSAGE="Hello World"

I want to access system variable MESSAGEin java.

我想MESSAGE在java中访问系统变量。

System.getenv("MESSAGE");doesn't work.

System.getenv("MESSAGE");不起作用。

回答by Andy Ross

The .bash_profile file is only sourced for login shells. If your java process is spawned from a shell that is not a login shell (for example a script with a #!/bin/shat the top) then it will not read it (though it may still inherit MESSAGE from the environment depending on how you run it).

.bash_profile 文件仅用于登录 shell。如果您的 java 进程是从不是登录 shell 的 shell 生成的(例如#!/bin/sh顶部带有 a 的脚本),那么它不会读取它(尽管它仍然可能从环境中继承 MESSAGE 取决于您如何运行它)。

Note also that .bash_profile is also not run for interactive shells that are not "login" shells, so you can't rely on it being run even when you have a shell prompt. People usually use .bashrc, which is sourced for all interactive shells, for that purpose.

另请注意,.bash_profile 也不会针对非“登录”shell 的交互式 shell 运行,因此即使您有 shell 提示,也不能依赖它正在运行。为此,人们通常使用 .bashrc,它是为所有交互式 shell 提供的。

If you want a variable to be set in all Bourne shell derivatives regardless of whether they are interactive or not, put it in both .profile and .bashrc.

如果您希望在所有 Bourne shell 衍生产品中设置一个变量,而不管它们是否是交互式的,请将它放在 .profile 和 .bashrc 中。

回答by Yair Zaslavsky

I would recommend you to test if your environment variable is indeed "defined" by using echo $MESSAGE as suggested above.
In addition, changing the bash_profile file will not effect your current shell,
you must use "source" in order for it to effect your current shell.
I would also recommend reading thisarticle about differences between bashrc and bash_profile.
Maybe you should define the EXPORT at bashrc?

我建议您使用上面建议的 echo $MESSAGE 来测试您的环境变量是否确实“定义”了。
此外,更改 bash_profile 文件不会影响您当前的 shell,
您必须使用“source”才能使其影响您当前的 shell。
我还建议阅读文章有关的.bashrc和.bash_profile中之间的差异。
也许您应该在 bashrc 中定义 EXPORT?

回答by user2039709

Another place to look at: /etc/environment(this may overrule/replace your .bashrc or .bash_profile in shells opened via your IDE)

另一个要查看的地方:/etc/environment(这可能会否决/替换通过 IDE 打开的 shell 中的 .bashrc 或 .bash_profile)

回答by Alex North-Keys

This actually gets even more interesting for users who have a .profile (for the old Bourne shell), that gets read in by .bash_profile automatically (providing compatability). In either case, the environment variables are read in once only at loginshell startup, and all subshells inherit those variables for free. The .bashrc is for tty-dependent things and unheritables like functions (the old sh used $ENV, I think, if it were set, for something similar).

对于拥有 .profile(对于旧的 Bourne shell)的用户来说,这实际上变得更加有趣,它会被 .bash_profile 自动读入(提供兼容性)。无论哪种情况,环境变量都只在登录shell 启动时读入一次,并且所有子 shell 都免费继承这些变量。.bashrc 用于依赖于 tty 的东西和不可继承的东西,比如函数(旧的 sh 使用 $ENV,我认为,如果它被设置,用于类似的东西)。

Your use of ~/.bash_profile looks fine (although single quotes are more reliable than double quotes, which allow some substitutions). Make sure you've logged out and back in between editing that file and trying your test, or use ". ~/.bash_profile" (no quotes and note the leading dot and space, since the dot is the command here).

您对 ~/.bash_profile 的使用看起来不错(尽管单引号比双引号更可靠,双引号允许进行一些替换)。确保在编辑该文件和尝试测试之间退出并重新登录,或使用“. ~/.bash_profile”(没有引号并注意前导点和空格,因为点是此处的命令)。

The article at http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.htmlcovers some good things, like using ". ~/.bashrc" at then end of your ~/.bash_profile (excepth that you should use -r, not -f). The comment about using export in your .bashrc is wrong, you should notdo this, for two reasons (1) an pretty insignificant performance penalty, (2) a pretty high chance that some command you execute won't get the environment variable - particularly things spawned from your window manager menus and other places where an actual command prompt didn't appear in any of their parents.

http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html 上的文章涵盖了一些好东西,比如在你的 ~/.bash_profile 末尾使用“. ~/.bashrc”(除了你应该使用 -r,而不是 -f)。关于在 .bashrc 中使用 export 的评论是错误的,你不应该这样做,有两个原因(1)非常微不足道的性能损失,(2)你执行的某些命令很可能不会获得环境变量 -特别是从你的窗口管理器菜单和其他地方产生的东西,在这些地方,实际的命令提示符没有出现在它们的任何父级中。

Lastly, make sure the $MESSAGE actually exists in your environment - look at the output of the "env" command - if it isn't there, the Java process won't see it unless it's internally creating it and storing it in its own internal copy of the environment variables.

最后,确保 $MESSAGE 确实存在于您的环境中 - 查看“env”命令的输出 - 如果它不存在,Java 进程将不会看到它,除非它在内部创建它并将其存储在自己的环境中环境变量的内部副本。

Another note, if you set env variables in the .profile, use this syntax:

另请注意,如果您在 .profile 中设置 env 变量,请使用以下语法:

VAR=VALUE ; export VAR

VAR=值;出口VAR

...since the old sh shell doesn't support "export VAR=VALUE". Using set -e before a bunch of these and set +e after can remove the need to use "export" at all, if I remember correctly.

...因为旧的 sh shell 不支持“export VAR=VALUE”。如果我没记错的话,在一堆这些之前使用 set -e 并在之后使用 set +e 可以完全消除使用“导出”的需要。