Bat, How to prevent environment variables leaking into parent process
Matthew Barrera
I have a bat file similiar to this
rem build.bat
rem add visual studio paths to env
call "%PROGRAMFILES(X86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
cl.exe blablaAfter running it a couple of times I get the error The input line is too long. because vcvarsall.bat seems to append to the path variables every time I invoke the script
I invoke the script with build.bat
I thought that the environment variables should not be saved between runs. Is there any way to make the variables not leak into the calling cmd shell?
23 Answers
You should localize the environment of your batch file by putting setlocal at the beginning and endlocal at the end (the latter may be omitted as it executes implicitly on exiting the script).
You could start the build.bat in a child process.
cmd /c build.bat
Variables in the parent process aren't affected by a child process.
0As far as I understand, environment variables don't get saved between runs, which is correct, but a "run" in this case is not the "run" of the batchfile, but of the command prompt in which you run the batchfile.
In my opinion, you have opened a command prompt. In there you launch the "build.bat" file, over and over again.
However, if you would launch the batchfile from outside of a command prompt (like double-clicking from the Windows explorer), this problem should not appear.