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?