Reputation: 57
Is it possible to modify an environment variable within a C program. Something like this:
#include <stdlib.h>
int main( void )
{
system( "echo $VARIABLE" );
system( "VARIABLE=somethig");
system( "echo $VARIABLE" );
return 0;
}
Upvotes: 2
Views: 238
Reputation: 17258
setenv(const char *name, const char *value, int overwrite);
is the function you need.
e.g. setenv("CONFIG_PATH", "/etc", 0);
From the man page:
DESCRIPTION
Thesetenv()
function adds the variablename
to the environment with the valuevalue
, ifname
does not already exist. Ifname
does exist in the environment, then its value is changed tovalue
if overwrite is nonzero; if overwrite is zero, then thevalue
ofname
is not changed. This function makes copies of the strings pointed to byname
andvalue
(by contrast withputenv(3)
).
Upvotes: 2