3

I would like to run a cmd.exe that would evaluate environment variables at call time instead of when it parses the command. If I set the BASE to 2 and echoing it, I should see the number 2, although running this script does not properly 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 . . .
phuclv
  • 37,963
  • 15
  • 156
  • 475
Steve
  • 1,145
  • 1
  • 11
  • 25

1 Answers1

5

This should work for you:

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

/V:ON enables Delayed Expansion of variables, which is what you need here. Order of the switches (/V:ON /C) matters.

Marc
  • 11,403
  • 2
  • 35
  • 45