The following code:
@IF [1]==[1] (
@SET test="This is a test"
echo --%test%--
)
When run twice, outputs the following:
----
--"This is a test"--
Why does this first one not have any value in the variable?
The following code:
@IF [1]==[1] (
@SET test="This is a test"
echo --%test%--
)
When run twice, outputs the following:
----
--"This is a test"--
Why does this first one not have any value in the variable?
The ()
is actually a line continuation, so the entire if
"line" is parsed (and variables substituted) before the set
command every executes. You need to use enabledelayedexpansion
:
@setlocal enabledelayedexpansion
@if [1]==[1] (
@set test="This is a test"
echo --!test!--
)