How do I find linux environment variable value using C or C++?
Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("test\n");
const char* s = getenv("PATH");
printf("PATH :%s\n",(s!=NULL)? s : "getenv returned NULL");
printf("end test\n");
}
Compile and Run
gcc getenv.c ; a.out
Example output
test
PATH :/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/opt/ucl/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/bin/X11:/opt/UCLCSdefunct/bin:/cs/research/crest/home0/ucacbbl/bin.intel:/cs/research/crest/home0/ucacbbl/bin:.
end test
W.B.Langdon
23 February 2012