title: mingw64 system("command") not same as "command" in shell
body: <p>A program "execinput" reads input lines from stdin
  and stores them into a character array "buffer" and then does this:</p>
  
  <pre class="lang-c prettyprint-override"><code>system(buffer);
  </code></pre>
  
  <p>Let "command" be any valid set of programs, parameters, and bash syntax.  Ideally the next two lines would give the same result (neglecting for the moment the handling of any double quotes within "command"):</p>
  
  <pre class="lang-none prettyprint-override"><code>command
  echo "command" | execinput
  </code></pre>
  
  <p>That is indeed the case on a linux system running in a bash shell.  However, in bash in an "Msys2 MingW 32bit" shell this happens (trailing semicolon after the 3 is intentional):</p>
  
  <pre class="lang-none prettyprint-override"><code>echo 1; echo 2; echo 3;
  1
  2
  3
  echo "echo 1; echo 2; echo 3;" | execinput
  1 ; echo 2; echo 3;
  echo "echo 1 &amp; echo 2 &amp; echo 3;" | execinput
  1
  2
  3;
  </code></pre>
  
  <p>The "&amp;" character is what cmd.exe uses to separate sub-commands.  That last command on a linux system, either directly in bash or through system() gives:</p>
  
  <pre class="lang-none prettyprint-override"><code>3
  1
  2
  </code></pre>
  
  <p>In the linux bash environment all the commands come out the same, whether run directly or via <code>system()</code> in <code>execinput()</code>. In the MSYS2 environment they don't.</p>
  
  <p>I believe that <code>COMSPEC</code> and <code>PATH</code> are involved somehow but having set the former like so:</p>
  
  <pre><code> export COMSPEC="C:\progs\msys32\usr\bin\bash.exe -c "
  </code></pre>
  
  <p>instead of the default COMSPEC value of:</p>
  
  <pre class="lang-none prettyprint-override"><code>C:\Windows\system32\cmd.exe
  </code></pre>
  
  <p>the results still were not the same.  </p>
  
  <p>Can somebody please explain what is going on here, and hopefully, how to make "command" come out the same directly on a bash command line and when invoked with <code>system()</code>?</p>
  
  <hr>
  
  <p>More info.  In a bash command line in MSYS2:</p>
  
  <pre><code>echo 'set' | execinput &gt; short.txt
  echo 'bash -c "set"' | execinput &gt; long.txt
  </code></pre>
  
  <p>then compare the file contents.  key differences are:</p>
  
  <blockquote>
    <ol>
    <li>long.txt has 11 BASH *symbols plus DIRSTACK, EUID, GROUPS, 
    IFS, MACHTYPE, OPTERR, OPTIND, OSTYPE, PPID, SHELLOPTS,UID.<br>
    Short.txt does not have these.</li>
    <li>SYSTEMROOT, COMSPEC, CONTITLE, HOMEPATH, and many others are 
    in single quotes in long.txt, no quotes in short.txt.<br>
    The strings are otherwise the same.</li>
    <li>PWD is in fully linux/posix syntax in long.txt and has its root
    at the top of the MSYS2 file system.  PWD is in hybrid syntax 
    in short.txt (starts with C: then has a / delimited path) and is
    the full Windows path.</li>
    <li>PATH in long.txt starts with linux/posix syntax entries,
    root at the top of the MSYS2 file system, then followed by entries
    like /c/Windows/System32.  Short.txt has entries which are in full
    Windows syntax.</li>
    <li>Long.txt has SHLVL=2, short.txt has SHLVL=1.</li>
    </ol>
  </blockquote>