5
Consider this example, where I start a new "sub" instance of cmd.exe
, and then try to set a new environment variable in it, and then check if it has the proper value set (this is in cmd.exe
of Windows 10):
>cmd /c "SET OPERATION=NEW & SET O"
OneDrive=C:\Users\user\OneDrive
OPERATION=NEW
OS=Windows_NT
So, as noted in https://superuser.com/a/776506/688965 - by doing SET O
I would "display all variables that begin with the letter 'O'", and indeed, it shows that an environment variable OPERATION
exists, and its value is NEW
. So far, so good.
However, if I try to print the environment value instead, it does not expand:
>cmd /c "SET OPERATION=NEW & echo %OPERATION%"
%OPERATION%
... however, if I try to expand any other environment variable, it works ?!:
>cmd /c "SET OPERATION=NEW & echo %OS%"
Windows_NT
Why does this happen? How can I set an environment variable, and then print it with echo
in a single line, in a cmd.exe
subshell?
3
"Why does this happen?" The %OPERATION% is executed before it has been set. See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details.
– DavidPostill – 2019-03-12T13:17:28.733Thanks @DavidPostill - feel free to post this as an answer, I'll accept it! – sdbbs – 2019-03-12T13:22:32.283