title: Iterate over a list of quoted strings
body: <p>I'm trying to run a for loop over a list of strings where some of them are quoted and others are not like so:</p>
  
  <pre><code>STRING='foo "bar_no_space" "baz with space"'
  for item in $STRING; do
      echo "$item"
  done
  </code></pre>
  
  <p>Expected result:</p>
  
  <pre><code>foo
  bar_no_space
  baz with space
  </code></pre>
  
  <p>Actual result:</p>
  
  <pre><code>foo
  "bar_no_space"
  "baz
  with
  space"
  </code></pre>
  
  <p>I can achieve the expected result by running the following command:</p>
  
  <pre><code>bash -c 'for item in '"$STRING"'; do echo "$item"; done;'
  </code></pre>
  
  <p><strong>I would like to do this without spawning a new bash process or using <code>eval</code></strong> because I do not want to take the risk of having random commands executed.</p>
  
  <p>Please note that <strong>I do not control the definition of the STRING variable, I receive it through an environment variable.</strong> So I can't write something like:</p>
  
  <pre><code>array=(foo "bar_no_space" "baz with space")
  for item in "${array[@]}"; do
      echo "$item"
  done
  </code></pre>
  
  <hr>
  
  <p>If it helps, what I am actually trying to do is split the string as a list of arguments that I can pass to another command.</p>
  
  <p>I have:</p>
  
  <pre><code>STRING='foo "bar_no_space" "baz with space"'
  </code></pre>
  
  <p>And I want to run:</p>
  
  <pre><code>my-command --arg foo --arg "bar_no_space" --arg "baz with space"
  </code></pre>