Your result is due to the fact that% name% is expanded during the parsing phase, and the entire line is parsed immediately to the set value.
You can get the current value on the same line as the set command in one of two ways.
1) use CALL to call ECHO% NAME% for analysis the 2nd time:
set name=value&call echo %^name%
I put ^
between percentages only if the name was already defined before the line was executed. Without a carriage, you get the old value.
Note: the source line had a space before &
, this space would be included in the value of the variable. You can prevent extra space by using quotation marks: set "name=value" &...
2) use a delayed extension to get the value at runtime, not during parsing. Most environments do not enable slow extension by default. You can enable slow extension on the command line with the appropriate CMD.EXE option.
cmd /v:on set "name=value" & echo !name!
A delayed extension can certainly be used on the command line, but is more commonly used in a batch file. SETLOCAL is used to enable slow extension in a batch file (it does not work from the command line)
setlocal enableDelayedExpansion set "name=value" & echo !name!
source share