在Linux中使用getenv函数 [英] Using getenv function in Linux
问题描述
我有以下简单的程序:
int main()
{
char* v = getenv("TEST_VAR");
cout << "v = " << (v==NULL ? "NULL" : v) << endl;
return 0;
}
这些行被添加到.bashrc文件:
These lines are added to .bashrc file:
TEST_VAR="2"
export TEST_VAR
现在,当我从终端窗口(Ubuntu 10.04),它打印v = 2.如果我以另一种方式运行程序:使用launcher或从Eclipse,它打印NULL。我认为这是因为TEST_VAR只定义在bash shell里面。
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. I think this is because TEST_VAR is defined only inside bash shell. How can I create persistent Linux environment variable, which is accessible in any case?
推荐答案
在我的系统(Fedora 13),你可以创建永久的Linux环境变量,通过将它们添加到/etc/profile.d /.
On my system (Fedora 13) you can make system wide environment variables by adding them under /etc/profile.d/.
来创建系统范围的环境变量因此,例如,如果将它添加到/etc/profile.d/中的文件my_system_wide.sh
So for example if you add this to a file in /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
这篇关于在Linux中使用getenv函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!