title: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
body: <p>I ran into <a href="https://ss64.com" rel="nofollow noreferrer">ss64.com</a> which provides good help regarding how to write batch scripts that the Windows Command Interpreter will run.</p>
  <p>However, I have been unable to find a good explanation of the <strong>grammar</strong> of batch scripts, how things expand or do not expand, and how to escape things.</p>
  <p>Here are sample questions that I have not been able to solve:</p>
  <ul>
  <li>How is the quote system managed? I made a <a href="http://tinyperl.sourceforge.net/" rel="nofollow noreferrer">TinyPerl</a> script<br />
  ( <code>foreach $i (@ARGV) { print '*' . $i ; }</code> ), compiled it and called it this way :
  <ul>
  <li><code>my_script.exe &quot;a &quot;&quot;b&quot;&quot; c&quot;</code> → output is  <code>*a &quot;b*c</code></li>
  <li><code>my_script.exe &quot;&quot;&quot;a b c&quot;&quot;&quot;</code> → output it <code>*&quot;a*b*c&quot;</code></li>
  </ul>
  </li>
  <li>How does the internal <code>echo</code> command work? What is expanded inside that command?</li>
  <li>Why do I have to use <code>for [...] %%I</code> in file scripts, but <code>for [...] %I</code> in interactive sessions?</li>
  <li>What are the escape characters, and in what context? How to escape a percent sign? For example, how can I echo <code>%PROCESSOR_ARCHITECTURE%</code> literally? I found that <code>echo.exe %&quot;&quot;PROCESSOR_ARCHITECTURE%</code> works, is there a better solution?</li>
  <li>How do pairs of <code>%</code> match? Example:
  <ul>
  <li><code>set b=a</code> , <code>echo %a %b% c%</code> → <code>%a a c%</code></li>
  <li><code>set a =b</code>, <code>echo %a %b% c%</code> → <code>bb% c%</code></li>
  </ul>
  </li>
  <li>How do I ensure a variable passes to a command as a single argument if ever this variable contains double quotes?</li>
  <li>How are variables stored when using the <code>set</code> command? For example, if I do <code>set a=a&quot; b</code> and then <code>echo.%a%</code> I obtain <code>a&quot; b</code>. If I however use <code>echo.exe</code> from the UnxUtils, I get <code>a b</code>. How comes <code>%a%</code> expands in a different way?</li>
  </ul>
  <p>Thank you for your lights.</p>