title: how to correctly use a bash variable containing multiple arguments (which may contain spaces)
body: <p>How can I pass a variable that contains spaces to a script so that the script processes them correctly?</p>
  <p>My minimal example:</p>
  <p>test.sh</p>
  <pre><code>#!/bin/bash
  COUNTER=1
  for i in &quot;$@&quot;; do echo &quot;$COUNTER '$i'&quot;; ((COUNTER++)); done
  </code></pre>
  <p>If I call the script directly with some arguments (escaped space) it works:</p>
  <pre><code>./test.sh 1 2 3\ 4 5
  </code></pre>
  <p>Output (as expected):</p>
  <pre><code>1 '1'
  2 '2'
  3 '3 4'
  4 '5'
  </code></pre>
  <p>If I store the arguments into a variable, the backslash is not interpreted correctly anymore as a escape char.</p>
  <pre><code>TEST_ARGS=&quot;1 2 3\ 4 5&quot;
  ./test.sh $TEST_ARGS
  </code></pre>
  <p>Output:</p>
  <pre><code>1 '1'
  2 '2'
  3 '3\'
  4 '4'
  5 '5'
  </code></pre>
  <p>I would like to get the same output like before. How can I reach it?</p>
  <pre><code>TEST_ARGS=&quot;1 2 3\ 4 5&quot;
  # how to call ./test.sh here?
  </code></pre>
  <p>My more detailed use case is, that I have some script, which prepares all flags for my other tools / scripts.</p>
  <p>prepare_flags.sh (very minimalistic):</p>
  <pre><code>#!/bin/bash
  echo 'TEST_ARGS=&quot;1 2 3\ 4 5&quot;'
  </code></pre>
  <p>In my actual script I source the output of the prepare_flags.sh script in order to access TEST_ARGS:</p>
  <pre><code>source &lt;( ./prepare_flags.sh )
  ./test.sh $TEST_ARGS # this fails
  bash -c &quot;./test.sh $TEST_ARGS&quot; # this works
  </code></pre>
  <p>Is there any other solution for this except the bash -c?</p>