Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set system variable from C++

This shell script

#!/bin/csh
set VAR=12345
echo $VAR

will peacefully give the output 12345 at shell. I need to use C++ to do the same in some part of the code:

string str = "12345";
retValue="set var1= "+str;      
system(retValue1.c_str());
system("echo $var1");

This doesn't create a system variable var1 and echos null which is understandable as each system function would create a child process with different environment variables. So I combine them as follows using just one system function...but it echos null again.

retValue="set var1= "+str;
retValue1=retValue+";\n echo $var1";
system(retValue1.c_str());

Can someone please guide me to set up the system variable thru C++. Thanks a lot in advance!

like image 730
Ankita Sharma Avatar asked Dec 07 '22 18:12

Ankita Sharma


1 Answers

Look at setenv in <cstdlib>:

#include <cstdlib>

setenv("VAR", "12345", true);
like image 128
Paul R Avatar answered Dec 23 '22 17:12

Paul R