title: How to loop through the sorted list of file names in two directories
body: <p>Please note, i have read entries like <a href="https://stackoverflow.com/questions/6122341/for-loop-for-files-in-multiple-folders-bash-shell">For loop for files in multiple folders - bash shell</a> and they ask for a significantly different thing.</p>
  
  <p>I want to loop through the file names in a sorted order that exist in either of two directories. Files can potentially have spaces in them.</p>
  
  <p>Let's say i have:</p>
  
  <pre><code>1/
    a
    a c b
    b
    c
  2/
    a
    d
  </code></pre>
  
  <p>I would want to loop through: 'a', 'a c b', 'b', 'c', 'd'.</p>
  
  <p>I have tried to do the following:</p>
  
  <pre><code>for fname in $((ls -1 -f -A "${dir1}"; ls -1 -f -A "${dir2}")|sort --unique); do
    echo "testing ${fname}"
  done
  </code></pre>
  
  <p>the result then is</p>
  
  <pre><code>testing .
  testing ..
  testing a
  testing a
  testing c
  testing b
  testing b
  testing c
  testing d
  </code></pre>
  
  <p>For whatever reason i am getting '.' and '..' entries, that i was trying to exclude with <code>-A</code>, and also the file 'a c b' gets broken down into three strings.</p>
  
  <p>I have tried to resolve it by adding <code>--zero</code> to the <code>sort</code> command, that changed nothing; by quoting the whole <code>$(ls...|sort)</code> part, and has resulted into a single entry into the for loop that has received the entire string with multiple lines each of which contained filename.</p>