title: Passing a space-separated System Property via a shell
body: <p>I'm having difficulties to startup a java program from a shell script (bash) where nested variables are used. Basically there are many system and -D java properties that need to be passed to a java program. I would like to organise them in a nicer way one below another as they are very difficult to read when in one line.</p>
  
  <p>This is similar to
  "<a href="https://stackoverflow.com/questions/8214392/passing-a-space-separated-system-property-via-a-shell-script-doesnt-work">Passing a space-separated System Property via a shell script doesn&#39;t work</a>" but not the same.</p>
  
  <p>Here is a stripped down sample. Imagine a java program like this:</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          for (String s : args) {
              System.out.println(s);
          }
      }
  }
  </code></pre>
  
  <p>When invoking it like this:</p>
  
  <p><code>java Main "First_Line" "Second line with spaces"</code></p>
  
  <p>It will give OK result like this response:</p>
  
  <blockquote>
  <pre><code>First_Line
  Second line with spaces
  </code></pre>
  </blockquote>
  
  <p>However if script like this is used:</p>
  
  <pre><code>#!/bin/sh
  PARAM01="FirstLine"
  PARAM02="Second line with spaces"
  PARAMS="$PARAM01 $PARAM02"
  java Main $PARAMS
  </code></pre>
  
  <p>Then all spaces are eaten and second parameter is passed unquoted to java. Result looks like this:</p>
  
  <blockquote>
  <pre><code>FirstLine
  Second
  line
  with
  spaces
  </code></pre>
  </blockquote>
  
  <p>Have tried to differently quote variables in a shell script, but so far without success. How to quote shell variables so that spaces are preserved and parameter is passed intact to java?</p>