System.getenv没有获取〜/.bash_profile中定义的变量 [英] System.getenv doesn't get variables defined in ~/.bash_profile
问题描述
这是〜/.bash_profile文件中的一行
export MESSAGE ="Hello World"
我想在Java中访问系统变量 MESSAGE
.
System.getenv("MESSAGE");
不起作用.
.bash_profile文件仅作为登录shell的来源.如果您的Java进程是从不是登录shell的shell中产生的(例如,顶部带有#!/bin/sh
的脚本),则它将不会读取它(尽管它仍然可以从环境中继承MESSAGE,具体取决于您的运行方式.
还请注意,对于不是登录" shell的交互式shell,也不会运行.bash_profile,因此即使有shell提示,也不能依靠它运行.人们通常使用.bashrc来实现该目的.
如果要在所有Bourne shell派生变量中设置变量,而不管它们是否是交互式的,请将其同时放在.profile和.bashrc中.
Here is a line in file ~/.bash_profile
export MESSAGE="Hello World"
I want to access system variable MESSAGE
in java.
System.getenv("MESSAGE");
doesn't work.
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/sh
at the top) then it will not read it (though it may still inherit MESSAGE from the environment depending on how you run it).
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.
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.
这篇关于System.getenv没有获取〜/.bash_profile中定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!