title: Bash: Expand braces and globs with spaces in filenames?
body: <p>I have some files that look like:</p>
  
  <pre><code>/path/with spaces/{a,b,c}/*.gz
  </code></pre>
  
  <p>And I need all files matching the glob under a subset of the <code>a,b,c</code> dirs to end up as arguments to a single command:</p>
  
  <pre><code>mycmd '/path/with spaces/a/1.gz' '/path/with spaces/a/2.gz' '/path/with spaces/c/3.gz' ...
  </code></pre>
  
  <p>The directories I care about come in as command line params and I have them in an array:</p>
  
  <pre><code>dirs=( "$@" )
  </code></pre>
  
  <p>And I want to do something like:</p>
  
  <pre><code>IFS=,
  mycmd "/path/with spaces/{${dirs[*]}}/"*.gz
  </code></pre>
  
  <p>but this doesn't work, because bash expands braces before variables.  I have tried tricks with <code>echo</code> and <code>ls</code> and even <code>eval</code> (*shudder*) but it's tough to make them work with spaces in filenames.  <code>find</code> doesn't seem to be much help because it doesn't do braces.  I can get a separate glob for each dir in an array with: </p>
  
  <pre><code>dirs=( "${dirs[@]/#//path/with spaces/}" )
  dirs=( "${dirs[@]/%//*.gz}" )
  </code></pre>
  
  <p>but then bash quotes the wildcards on expansion.</p>
  
  <p>So: is there an elegant way to get all the files matching a variable brace and glob pattern, properly handling spaces, or am I stuck doing for loops?  I'm using Bash 3 if that makes a difference.</p>