Your example set A="qwerty" && echo %A%
isn't correct. Variables in the cmd
prompt / a batch file are expanded once per line / command:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && echo %A%
%A%
==> echo %A%
"qwerty"
The SET
command was first introduced with MS-DOS 2.0 in March 1983,
at that time memory and CPU were very limited and the expansion of
variables once per line was enough.
A workaround using the CALL
command:
==> set "A="
==> echo %A%
%A%
==> set A="qwerty" && CALL echo %A%
"qwerty"
Edit:
For the sake of completeness, the following batch script shows the mechanism of percent expansion and its combination with the CALL
command in detail (note doubled %
percent signs in the batch file CALL Echo %%_var%%
):
@ECHO OFF
SETLOCAL
if NOT "%~1"=="" ECHO ON
echo 1st:
Set "_var=first"
Set "_var=second" & Echo %_var% & CALL Echo %%_var%%
echo 2nd:
Set "_var=first"
Set "_var=second" & CALL Echo %%_var%% & Echo %_var%
Output, echo OFF
:
==> D:\bat\SO\55237418.bat
1st:
first
second
2nd:
second
first
Output, echo ON
:
==> D:\bat\SO\55237418.bat on
==> echo 1st:
1st:
==> Set "_var=first"
==> Set "_var=second" & Echo first & CALL Echo %_var%
first
second
==> echo 2nd:
2nd:
==> Set "_var=first"
==> Set "_var=second" & CALL Echo %_var% & Echo first
second
first