when I’m running this script (from a .bat file):
set var1=true
if "%var1%"=="true" (
set var2=myvalue
echo %var2%
)
I always get:
ECHO is on.
Meaning the var2 variable was not really set.
Can anyone please help me understand why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
var2 is set, but the expansion in the line
echo %var2%occurs before the block is executed.At this time
var2is empty.Therefore the delayedExpansion syntax exists, it uses
!instead of%and it is evaluated at execution time, not parse time.Please note that in order to use
!, the additional statementsetlocal EnableDelayedExpansionis needed.