c++linuxenvironment-variablessetenv

C++ setenv resolve other variables


I am using c++ and setenv to set a variable like in this program below:

setenv("TEST", "$HOME/test", 1);
char* val = getenv("TEST");
printf("TEST=%s", val);

The output I get is "TEST=$HOME/test". However I want the output be like "TEST=/home/toboxos/test". I found nothing using the linux manual. Is there any function resolving the environment variables or have I to do this by myself?


Solution

  • This substitution you're expecting is a feature of your shell, it's not inherent to the environment.

    You need to getenv("HOME") yourself, concatenate it with "/test", and use the result as input to setenv.