title: How do I pass a bash parameter that includes a directory with a space as a Java argument?
body: <p>I am currently attempting to make a bash script that executes a Java main class with a path to an archive file as an argument. I want to make sure that it can handle spaces in that path, which is giving me a bit of a headache.</p>
  
  <p>I want to set that path as an environment variable and call it when I execute the main class, so the script looks something like this:</p>
  
  <pre><code>export ArgPath='/Foo Path/foo.zip'
  
  $JAVA_HOME/bin/java -cp ./foo.jar foopackage.main -a $ArgPath
  </code></pre>
  
  <p>Unfortunately, when I execute this in Bash, the command comes out like this:</p>
  
  <pre><code>/foo_Java_Home/bin/java -cp ./foo.jar foopackage.main -a /Foo Path/foo.zip
  </code></pre>
  
  <p>Java arguments can't have spaces. Java treats "/Foo" and "Path/foo.zip" as two separate arguments. Okay, that's not a problem. Java can handle arguments with spaces. I need to find some way to get double or single quotes around it. I'll try to escape double quotes around the path. The script now looks like this:</p>
  
  <pre><code>export ArgPath='\"/Foo Path/foo.zip\"'
  
  $JAVA_HOME/bin/java -cp ./foo.jar foopackage.main -a $ArgPath
  </code></pre>
  
  <p>Bash gives me this...</p>
  
  <pre><code>/foo_Java_Home/bin/java -cp ./foo.jar foopackage.main -a '\"/Foo' 'Path/foo.zip\"'
  </code></pre>
  
  <p>...which still causes Java to treat them as two separate arguments (not to mention that it wouldn't work as a single argument anyway). I've tried about every combination and permutation of using single quotes, double quotes, and escaping single quotes/double quotes/spaces I can think of. Is there any way to provide the desired output of</p>
  
  <pre><code>'/Foo Path/foo.zip'
  </code></pre>
  
  <p>or
      "/Foo Path/foo.zip"</p>
  
  <p>In Bash? For that matter, is it possible to get Bash to accept an arbitrary string literal without trying to add anything to it in circumstances like these? Escaping characters doesn't seem to work, so that may be the only option if it's possible.</p>
  
  <p>Thanks in advance. </p>