I have a C program that prints every environment variable whose name is given by stdin
.
It prints variables such as $PATH
, $USER but it doesn't see the environment variables that I defined myself in the Linux shell. For instance, in ~/.bashrc
I exported MYTEST=test_is_working
, then I sourced the .bashrc
(source ~/.bashrc
).
I expected the program to output "test_is_working" with getenv
but it doesn't.
#include <QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
char* my_env= getenv("MYTEST");
if(my_env!=NULL){
printf("my env is : %s \n", my_env);
}
else {
printf("can't find env \n");
}
return a.exec();
}
It outputs: "can't find env".
While when I open a terminal and enter "env", I have MYTEST=test_is_working
I saw a similar post: Using getenv function where the solution is to launch the program from the shell. But I can't because I'm running and debugging in Qtcreator.
I don't know where I'm in the wrong, could someone explain it to me?
Thanks.