Question

I am new in linux.I have problem in accessing my defined environment varibales in C program.

I have defined one variable in linux command terminal as follows:

 $   ExampleVar="Hi"

And in C program I am trying to access it using

getenv("ExampleVar")

But it is null every time. When I try to access other environment variables like USER, getenv gives correct results. I have also tried extern collection of unistd.h

It is not showing ExampleVar too.

Please help me.

Was it helpful?

Solution

It depends upon your shell. If you use bash -see bash(1) for more- type

 export EXAMPLEVAR="Hi"

in the shell (e.g. in the terminal before running your program), then use getenv("EXAMPLEVAR") in your C program. See getenv(3)

If you don't export a bash variable foo, you still can use $foo in your bash commands, but getenv("foo") would fail and return NULL from inside compiled C programs.

Conventionally, environment variables have full capital names. See environ(7)

See also env(1) command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top