title: Command works in interactive shell but not shell script
body: <p>I wrote a script that would generate the appropriate arguments to javac to compile my project in an effort to grow more proficient at shell scripting. </p>
  
  <p>The weird this is.. The script works perfectly, but if the script runs javac with those parameters it doesn't work, and if I run the <strong>exact</strong> same command in the interactive shell it does. Everything is outputted with absolute paths, so I'm pretty much at a loss here.</p>
  
  <p>Example directory structure:</p>
  
  <pre><code>src/File.java
  src/File.png
  src/dir/File2.java
  jars/Library.jar
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>build/File.class
  build/File.png
  build/dir/File2.class
  </code></pre>
  
  <p>The shell script:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>#! /bin/sh
  
  cwd=$(pwd)
  
  if [ -d "build" ]; then
      rm -rf $cwd/build/*
  else
      mkdir $cwd/build
  fi
  
  find $cwd/src \( ! -path '*/.*' \) -type f ! -iname "*.java" | xargs -I{} cp --parents {} $cwd/build
  
  cmd=$(echo javac -sourcepath $cwd/src -classpath $(find $cwd/jars -type f | awk '{ printf("\"%s\";", $0);}'  | awk '{ print substr($0, 0, length($0)); }') -d $cwd/build $(find $cwd/src \( ! -path '*/.*' \) -type f -iname "*.java"))
  
  $cmd
  echo $cmd
  </code></pre>
  
  <p>Command output:</p>
  
  <pre><code>javac -sourcepath /home/test/src -classpath "/home/test/jars/Library.jar" -d /home/test/build /home/test/src/File.java /home/test/src/dir/File2.java
  </code></pre>
  
  <p>My actual project is too large to post here, but basically what happens is I get a huge amount of error output, as if the classpath was improperly set (they're errors on library functions). If I copy the command from the echo statement, paste it, and hit enter it works perfectly.</p>
  
  <p>I don't get it.</p>
  
  <p>Any ideas?</p>