Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove environmental variable programmatically

I need to write a unit test for some C++ code that checks for the presence of an environmental variable. I'm using MSVS 2008 and gtest as my framework. I add the environmental variable using putenv, I check the environmental variable using getevn, but I can't figure out how to remove it so that no other test will see it. I realize this is probably easy, but I can't find the answer on the internet. Thanks

like image 652
Steve Avatar asked Jul 08 '10 15:07

Steve


People also ask

How do you remove an environment variable?

Delete Environment Variables You need to just use the unset command with the variable name to delete it. This command will remove the variable permanently.

Which command remove variable from shell script?

Using the env Command Here, we're using the -i flag, which clears the variables exported by the user and starts a new session of Bash with default values.

How do you clear an environment variable in Linux?

Now that you have set many environment variables on your system, you may want to unset some of them if you don't use them anymore. On Linux, there are two ways of unsetting environment variables : by using the unset command or by deleting variable entries into your system files.


2 Answers

Calling putenv again specifying "SOME_VAR=" as parameter will delete environment variable SOME_VAR. btw, Microsoft recommends using _putenv as putenv is deprecated.

like image 108
341008 Avatar answered Sep 28 '22 02:09

341008


You could always fork/exec a subprocess to do just the putenv/getenv testing, and then when it terminates there isn't any stray environment left around.

like image 21
Mark B Avatar answered Sep 28 '22 02:09

Mark B