title: Passing arguments to bash -c with spaces
body: <p>I have the command line arguments to a program stored in a bash array and one of the arguments can have spaces in it. <code>X=(A B "C D" E)</code></p>
  
  <p>I have another constraint that I need to execute my program through a <code>bash -c</code> statement. (Technically it is a <code>... | xargs bash -c "./a.out ..."</code> statement)</p>
  
  <p>When I do this, I end up mangling the spaces in my command. How can I do this correctly?</p>
  
  <p>I wrote a quick c++ program to output command line arguments to represent my program</p>
  
  <pre><code>#include &lt;iostream&gt;
  int main(int argc, char** argv)
  {
      for (int i = 1; i &lt; argc; ++i)
          std::cout &lt;&lt; '"' &lt;&lt; argv[i] &lt;&lt; '"' &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>I am looking to get the output from <code>./a.out "${X[@]}"</code></p>
  
  <pre><code>"A"
  "B"
  "C D"
  "E"
  </code></pre>
  
  <p>but when I test this <code>bash -c "./a.out ${X[@]}"</code> I just get just get</p>
  
  <pre><code>"A"
  </code></pre>
  
  <p>and when I test this <code>bash -c "./a.out ${X[*]}"</code> it mangles the spaces and I get</p>
  
  <pre><code>"A"
  "B"
  "C"
  "D"
  "E"
  </code></pre>
  
  <p>What can  Ido?</p>