batch-filecmddelegateschild-process

How to delegate variable evaluation to the child CMD process in Windows command-line?


I am trying to delegate variable evaluation to the child CMD process in Windows command-line. Consider the following:

set foo=bar
cmd /c "set foo=rod & echo %foo%"

It prints bar and not rod since the expressions %foo% is evaluated before the statement itself. I want to be the other way around so it would instead print rod. How to do so?


Solution

  • Enable delayed expansion and use it

    set foo=bar
    cmd /V:on /c "set foo=rod & echo !foo!"