What you're doing is the correct way to do it, but it will be applicable only to the current process and possibly its child processes.
When you check the environment variable outside your program, it will show you the old value.
On Windows you can do the same using the
SetEnvironmentVariable[
^] API.