Expanding environment variables for command prompt

别说谁变了你拦得住时间么 提交于 2019-12-04 04:04:17

问题


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 . . .


回答1:


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.



来源:https://stackoverflow.com/questions/9844311/expanding-environment-variables-for-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!