Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement