Using getenv on Linux

I have the following simple program:

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

These lines are added to the .bashrc file:

TEST_VAR = "2"
export TEST_VAR

Now, when I run this program from a terminal window (Ubuntu 10.04), it prints v = 2. If I run the program differently: using the launcher or from Eclipse, it prints NULL. I think this is due to the fact that TEST_VAR is defined only inside the bash shell. How to create a constant Linux environment variable, accessible anyway?

+3
source share
4

(Fedora 13) , /etc/profile.d/.

, , /etc/profile.d/my_system_wide.sh

SYSTEM_WIDE="system wide"
export SYSTEM_WIDE

, , , .

echo $SYSTEM_WIDE
system_wide
+3

.bash_profile ( ). , .

, bash, :

export TEST_VAR="2"
+2

, , .bash_profile , bash ?

Linux ksh. .profile, , . * .

:)

+2

Linux , . . , - , , var . , var, .

. , .bash_profile , , , . /etc/profile bash .

+1

Source: https://habr.com/ru/post/1754544/


All Articles