简体   繁体   English

在Linux中使用getenv函数

[英]Using getenv function in Linux

I have this following simple program: 我有以下简单的程序:

int main()
{
    char* v = getenv("TEST_VAR");
    cout << "v = " << (v==NULL ? "NULL" : v) << endl;
    return 0;
}

These lines are added to .bashrc file: 这些行被添加到.bashrc文件中:

TEST_VAR="2"
export TEST_VAR

Now, when I run this program from the terminal window (Ubuntu 10.04), it prints v = 2. If I run the program by another way: using launcher or from Eclipse, it prints NULL. 现在,当我从终端窗口(Ubuntu 10.04)运行该程序时,它打印v = 2.如果我以另一种方式运行程序:使用启动程序或从Eclipse,它打印NULL。 I think this is because TEST_VAR is defined only inside bash shell. 我认为这是因为TEST_VAR仅在bash shell中定义。 How can I create persistent Linux environment variable, which is accessible in any case? 如何创建持久的Linux环境变量,无论如何都可以访问?

On my system (Fedora 13) you can make system wide environment variables by adding them under /etc/profile.d/. 在我的系统(Fedora 13)上,您可以通过在/etc/profile.d/下添加它们来创建系统范围的环境变量。

So for example if you add this to a file in /etc/profile.d/my_system_wide.sh 例如,如果将其添加到/etc/profile.d/my_system_wide.sh中的文件中

SYSTEM_WIDE="system wide"
export SYSTEM_WIDE

and then open a another terminal it should source it regardless of who the user is opening the terminal 然后打开另一个终端,无论用户打开终端是谁,它都应该来源

echo $SYSTEM_WIDE
system_wide

Add that to .bash_profile (found in your home directory). 将其添加到.bash_profile (在主目录中找到)。 You will need to log out and log back in for it to take effect. 您需要注销并重新登录才能生效。

Also, since you are using bash, you can combine the export and set in a single statement: 此外,由于您使用的是bash,因此可以在单个语句中组合导出和设置:

export TEST_VAR="2"

Sorry if I'm being naive but isn't .bash_profile useful only if you are running bash as your default shell ? 对不起,如果我天真但是.bash_profile只有在你运行bash作为默认shell时才有用吗?

I 'sometimes' use Linux and mostly use ksh. 我'有时'使用Linux并且大多使用ksh。 I have .profile so may be you should check for .*profile and export the variable there. 我有.profile所以你应该检查。* profile并在那里导出变量。

Good luck :) 祝好运 :)

There is no such thing as a system-wide environment variable on Linux. 在Linux上没有系统范围的环境变量。 Every process has its own environment. 每个流程都有自己的环境。 Now by default, every process inherits its environment from its parent, so you can get something like a system-wide environment by ensuring that a var is set in an ancestor of every process of interest. 现在默认情况下,每个进程都从其父进程继承其环境,因此您可以通过确保在每个感兴趣的进程的祖先中设置var来获得类似系统范围的环境。 Then, as long as no other process changes that var, every process of interest will have it set. 然后,只要没有其他进程更改该var,每个感兴趣的进程都会设置它。

The other answers here give various methods of setting variables early. 这里的其他答案提供了各种早期设置变量的方法。 For example, .bash_profile sets it in every login process a user runs, which is the ultimate parent of every process they run after login. 例如, .bash_profile在用户运行的每个登录进程中设置它,这是他们登录后运行的每个进程的最终父进程。 /etc/profile is read by every bash login by every user. /etc/profile由每个用户的每次bash登录读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM