I was writing a unit test for a script file and noticed that after clearing all the environment variables, set
produces no output, yet when I iterate over the set
output in a for /f
loop, I always get COMSPEC, PATHEXT and PROMPT.
@setlocal
@set prompt=:
@for /f "tokens=1,2 delims==" %%a in ('set') do @set "%%a="
set
@for /f "tokens=1,2 delims==" %%a in ('set') do if "%%a" neq "" @echo %%a %%b
set
@exit /b 878470
Produces:
:test
D:\TMP\Joseph>set
D:\TMP\Joseph>if "COMSPEC" NEQ ""
COMSPEC C:\WINDOWS\system32\cmd.exe
D:\TMP\Joseph>if "PATHEXT" NEQ ""
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC
D:\TMP\Joseph>if "PROMPT" NEQ ""
PROMPT $P$G
D:\TMP\Joseph>set
:
I discovered this with a variable counter routine used to verify that my clear variables routine was working correctly. Easy enough to work around, but I'd be interested to know if this is a Win10 only thing or is it a historical feature of cmd.exe?
EDIT: Seems we've established that every new instance of cmd.exe adds COMSPEC, PATHEXT AND PROMPT to its environment if they were missing from its parent environment. Is this special behavior officially defined anywhere or just another one of those undocumented behaviors of cmd.exe?