Set and print content of environment variable in cmd.exe subshell?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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:UsersuserOneDrive
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?
windows command-line cmd.exe environment-variables
add a comment |
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:UsersuserOneDrive
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?
windows command-line cmd.exe environment-variables
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♦
Mar 12 at 13:17
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22
add a comment |
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:UsersuserOneDrive
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?
windows command-line cmd.exe environment-variables
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:UsersuserOneDrive
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?
windows command-line cmd.exe environment-variables
windows command-line cmd.exe environment-variables
asked Mar 12 at 12:13
sdbbssdbbs
1677
1677
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♦
Mar 12 at 13:17
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22
add a comment |
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♦
Mar 12 at 13:17
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22
3
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♦
Mar 12 at 13:17
"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♦
Mar 12 at 13:17
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22
add a comment |
2 Answers
2
active
oldest
votes
Why does this happen?
The %OPERATION%
is expanded (Phase 1) before the variable has been set (Phase 7).
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details:
Processing a line of code in a batch file involves multiple phases.
Here is a brief overview of the various phases:
Phase 0) Read Line:
Phase 1) Percent Expansion:
Phase 1.5) Remove : Remove all Carriage Return (0x0D) characters
Phase 2) Process special characters, tokenize, and build a cached
command block: This is a complex process that is affected by things
such as quotes, special characters, token delimiters, and caret
escapes.
Phase 3) Echo the parsed command(s) Only if the command block did not
begin with @, and ECHO was ON at the start of the preceding step.
Phase 4) FOR %X variable expansion: Only if a FOR command is active
and the commands after DO are being processed.
Phase 5) Delayed Expansion: Only if delayed expansion is enabled
Phase 5.3) Pipe processing: Only if commands are on either side of a
pipe
Phase 5.5) Execute Redirection:
Phase 6) CALL processing/Caret doubling: Only if the command token is
CALL
Phase 7) Execute: The command is executed
add a comment |
How can I set an environment variable, and then print it with echo in a single line, in a cmd.exe subshell?
To enable neccessary delayed expansion in a cmd subshell you'll have to use the /V:ON
switch of cmd.exe and use an !
exclamation mark instead of the %
percent sign.
> cmd /V:ON /C "SET OPERATION=NEW&echo [!OPERATION!]"
[NEW]
Just a reminder
The trailing space in your original set will be part of the variable content:
> cmd /V:ON /C "SET OPERATION=NEW &echo [!OPERATION!]"
[NEW ]
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "3"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1413376%2fset-and-print-content-of-environment-variable-in-cmd-exe-subshell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why does this happen?
The %OPERATION%
is expanded (Phase 1) before the variable has been set (Phase 7).
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details:
Processing a line of code in a batch file involves multiple phases.
Here is a brief overview of the various phases:
Phase 0) Read Line:
Phase 1) Percent Expansion:
Phase 1.5) Remove : Remove all Carriage Return (0x0D) characters
Phase 2) Process special characters, tokenize, and build a cached
command block: This is a complex process that is affected by things
such as quotes, special characters, token delimiters, and caret
escapes.
Phase 3) Echo the parsed command(s) Only if the command block did not
begin with @, and ECHO was ON at the start of the preceding step.
Phase 4) FOR %X variable expansion: Only if a FOR command is active
and the commands after DO are being processed.
Phase 5) Delayed Expansion: Only if delayed expansion is enabled
Phase 5.3) Pipe processing: Only if commands are on either side of a
pipe
Phase 5.5) Execute Redirection:
Phase 6) CALL processing/Caret doubling: Only if the command token is
CALL
Phase 7) Execute: The command is executed
add a comment |
Why does this happen?
The %OPERATION%
is expanded (Phase 1) before the variable has been set (Phase 7).
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details:
Processing a line of code in a batch file involves multiple phases.
Here is a brief overview of the various phases:
Phase 0) Read Line:
Phase 1) Percent Expansion:
Phase 1.5) Remove : Remove all Carriage Return (0x0D) characters
Phase 2) Process special characters, tokenize, and build a cached
command block: This is a complex process that is affected by things
such as quotes, special characters, token delimiters, and caret
escapes.
Phase 3) Echo the parsed command(s) Only if the command block did not
begin with @, and ECHO was ON at the start of the preceding step.
Phase 4) FOR %X variable expansion: Only if a FOR command is active
and the commands after DO are being processed.
Phase 5) Delayed Expansion: Only if delayed expansion is enabled
Phase 5.3) Pipe processing: Only if commands are on either side of a
pipe
Phase 5.5) Execute Redirection:
Phase 6) CALL processing/Caret doubling: Only if the command token is
CALL
Phase 7) Execute: The command is executed
add a comment |
Why does this happen?
The %OPERATION%
is expanded (Phase 1) before the variable has been set (Phase 7).
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details:
Processing a line of code in a batch file involves multiple phases.
Here is a brief overview of the various phases:
Phase 0) Read Line:
Phase 1) Percent Expansion:
Phase 1.5) Remove : Remove all Carriage Return (0x0D) characters
Phase 2) Process special characters, tokenize, and build a cached
command block: This is a complex process that is affected by things
such as quotes, special characters, token delimiters, and caret
escapes.
Phase 3) Echo the parsed command(s) Only if the command block did not
begin with @, and ECHO was ON at the start of the preceding step.
Phase 4) FOR %X variable expansion: Only if a FOR command is active
and the commands after DO are being processed.
Phase 5) Delayed Expansion: Only if delayed expansion is enabled
Phase 5.3) Pipe processing: Only if commands are on either side of a
pipe
Phase 5.5) Execute Redirection:
Phase 6) CALL processing/Caret doubling: Only if the command token is
CALL
Phase 7) Execute: The command is executed
Why does this happen?
The %OPERATION%
is expanded (Phase 1) before the variable has been set (Phase 7).
See How does the Windows Command Interpreter (CMD.EXE) parse scripts? for the gory details:
Processing a line of code in a batch file involves multiple phases.
Here is a brief overview of the various phases:
Phase 0) Read Line:
Phase 1) Percent Expansion:
Phase 1.5) Remove : Remove all Carriage Return (0x0D) characters
Phase 2) Process special characters, tokenize, and build a cached
command block: This is a complex process that is affected by things
such as quotes, special characters, token delimiters, and caret
escapes.
Phase 3) Echo the parsed command(s) Only if the command block did not
begin with @, and ECHO was ON at the start of the preceding step.
Phase 4) FOR %X variable expansion: Only if a FOR command is active
and the commands after DO are being processed.
Phase 5) Delayed Expansion: Only if delayed expansion is enabled
Phase 5.3) Pipe processing: Only if commands are on either side of a
pipe
Phase 5.5) Execute Redirection:
Phase 6) CALL processing/Caret doubling: Only if the command token is
CALL
Phase 7) Execute: The command is executed
answered Mar 12 at 13:26
DavidPostill♦DavidPostill
108k27235271
108k27235271
add a comment |
add a comment |
How can I set an environment variable, and then print it with echo in a single line, in a cmd.exe subshell?
To enable neccessary delayed expansion in a cmd subshell you'll have to use the /V:ON
switch of cmd.exe and use an !
exclamation mark instead of the %
percent sign.
> cmd /V:ON /C "SET OPERATION=NEW&echo [!OPERATION!]"
[NEW]
Just a reminder
The trailing space in your original set will be part of the variable content:
> cmd /V:ON /C "SET OPERATION=NEW &echo [!OPERATION!]"
[NEW ]
add a comment |
How can I set an environment variable, and then print it with echo in a single line, in a cmd.exe subshell?
To enable neccessary delayed expansion in a cmd subshell you'll have to use the /V:ON
switch of cmd.exe and use an !
exclamation mark instead of the %
percent sign.
> cmd /V:ON /C "SET OPERATION=NEW&echo [!OPERATION!]"
[NEW]
Just a reminder
The trailing space in your original set will be part of the variable content:
> cmd /V:ON /C "SET OPERATION=NEW &echo [!OPERATION!]"
[NEW ]
add a comment |
How can I set an environment variable, and then print it with echo in a single line, in a cmd.exe subshell?
To enable neccessary delayed expansion in a cmd subshell you'll have to use the /V:ON
switch of cmd.exe and use an !
exclamation mark instead of the %
percent sign.
> cmd /V:ON /C "SET OPERATION=NEW&echo [!OPERATION!]"
[NEW]
Just a reminder
The trailing space in your original set will be part of the variable content:
> cmd /V:ON /C "SET OPERATION=NEW &echo [!OPERATION!]"
[NEW ]
How can I set an environment variable, and then print it with echo in a single line, in a cmd.exe subshell?
To enable neccessary delayed expansion in a cmd subshell you'll have to use the /V:ON
switch of cmd.exe and use an !
exclamation mark instead of the %
percent sign.
> cmd /V:ON /C "SET OPERATION=NEW&echo [!OPERATION!]"
[NEW]
Just a reminder
The trailing space in your original set will be part of the variable content:
> cmd /V:ON /C "SET OPERATION=NEW &echo [!OPERATION!]"
[NEW ]
edited Mar 12 at 22:58
answered Mar 12 at 13:33
LotPingsLotPings
5,2711823
5,2711823
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1413376%2fset-and-print-content-of-environment-variable-in-cmd-exe-subshell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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♦
Mar 12 at 13:17
Thanks @DavidPostill - feel free to post this as an answer, I'll accept it!
– sdbbs
Mar 12 at 13:22