0

I used the setenv() function to add a new env variable — ex:

setenv("HELLO", "env", 1)

Why can't I find the new environment variable I've created whenever I type the env command in the terminal?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
haxor12
  • 21
  • 1
  • 8
  • Because the environment is specific for each process, and when you run a program it's run as its own process. – Some programmer dude Dec 28 '18 at 17:30
  • If you ran `system("env")` in your program that calls `setenv()`, you should see the new value of `HELLO=env` in the environment. But the program cannot sensibly set the environment of the the program (shell?) that invoked it. – Jonathan Leffler Dec 28 '18 at 17:43

1 Answers1

1

Every process has its own set of environment variables. When you programmatically set them for a particular process, you are only setting them for that process (and any processes it runs). When the process exits, then those settings disappear.

If you want for your terminal session you need to set in the command shell for the terminal session.

See also How to use setenv() to export a variable in c++?

Also see Why the environment variable is unset after using setenv( )

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106