title: Groovy execute complicated shell command
body: <p>I would like to execute a shell command within a Groovy script where part of the script is variable of a filename which may contain spaces.</p>
  
  <p>According to <a href="https://stackoverflow.com/questions/786160/groovy-execute-with-parameters-containing-spaces">groovy execute with parameters containing spaces</a> I may use an array for this:</p>
  
  <pre><code>def filename = '/tmp/folder with spaces'
  
  // does not work
  'ls "' + filename + '"'.execute()
  
  // works
  ['ls', filename].execute()
  </code></pre>
  
  <p>But I can't figure out how to reformat the following command using an array where "filename" is a groovy variable containing a filename with or without spaces:</p>
  
  <pre><code>mdfind "$(/usr/libexec/PlistBuddy -c 'Print RawQuery' filename)"
  </code></pre>
  
  <p>This does not work:</p>
  
  <pre><code>// not working
  ["mdfind", "\$(/usr/libexec/PlistBuddy -c 'Print RawQuery' " + filename + ")"].execute()
  ["mdfind", "\$(/usr/libexec/PlistBuddy -c 'Print RawQuery' ", filename, ")"].execute()
  </code></pre>