Expanding environment variables for the command line

I would like to run cmd.exe, which will evaluate environment variables at the time of the call, not when it parses the command. If I set BASE to 2 and repeat it, I should see number 2, although running this script does not correctly set the base.

Expected Behavior: C:\Users\schristo>cmd.exe /X /C "set BASE=2 && echo %BASE% && pause" 2 Press any key to continue . . .

Actual behavior: C:\Users\schristo>cmd.exe /X /C "set BASE=2 && echo %BASE% && pause" %BASE% Press any key to continue . . .


source to share

1 answer

This should work for you:

cmd.exe /X /V:ON /C "set BASE=2&&echo !BASE!&&pause"


allows Delayed Expansion of variables, which is what you need here. The order of the switches ( /V:ON /C

) matters.



All Articles