title: how to set JVM property line.separator to newline from bash
body: <p>I want to set JVM System property <code>line.separator</code> to a single newline on Windows (it's already a single newline by default everywhere else).</p>
  
  <p>This scala script is used to display the effective line.separator property:</p>
  
  <pre><code>#!/usr/bin/env scala
  val bytes = sys.props("line.separator").map { _.toInt }.mkString(" ")
  printf("line.separator bytes: %s\n",bytes)
  </code></pre>
  
  <p>On a windows JVM, it normally prints the following:</p>
  
  <pre><code>line.separator bytes: 13 10
  </code></pre>
  
  <p>Briefly, I'm looking for a way to launch my test script so that it prints the following output:</p>
  
  <pre><code>line.separator bytes: 10
  </code></pre>
  
  <p>I can achive this with the following JAVA_OPTS setting:</p>
  
  <pre><code>export JAVA_OPTS=-Dline.separator=$'\n'
  </code></pre>
  
  <p>but only if I also modify the standard scala script by surrounding $JAVA_OPTS with double quotes.  Here's the section near the end of the scala script verbatim (i.e., WITHOUT the needed modification):</p>
  
  <pre><code>execCommand \
    "${JAVACMD:=java}" \
    $JAVA_OPTS \
    "${java_args[@]}" \
    "${classpath_args[@]}" \
    -Dscala.home="$SCALA_HOME" \
    $OVERRIDE_USEJAVACP \
    $WINDOWS_OPT \
     scala.tools.nsc.MainGenericRunner  "$@"
  </code></pre>
  
  <p>With these two modifications, the test script above prints the following:</p>
  
  <pre><code>$ reportLineSeparator.sc
  line.separator bytes: 10
  </code></pre>
  
  <p>Adding quotes to JAVA_OPTS is not a viable option, however, since that would prevent it from being unset, or from specifying multiple settings.</p>
  
  <p>So the requirement seems to be to somehow arrange for the unquoted JAVA_OPTS to be correctly handled without losing the encoded newline.</p>
  
  <p>I'm starting to suspect that there is solution, although I'm hopeful that someone will prove me wrong.</p>
  
  <p>Update: it seems that if a bash array is used instead of JAVA_OPTS, that would provide a solution, since it could be quoted in the scala script.   In other words, replace the unquoted $JAVA_OPTS above with this:</p>
  
  <pre><code>"${JAVA_OPTS_ARR[@]}" \
  </code></pre>
  
  <p>I was pleasantly surprised that it also doesn't cause problems when JAVA_OPTS_ARR is undefined.</p>
  
  <p>However, this isn't a viable solution because it's not possible to export bash arrays (see <a href="https://stackoverflow.com/questions/5564418/exporting-an-array-in-bash-script">Exporting an array in bash script</a>)</p>
  
  <p>Follow-up: after further thinking about this problem, I concluded that interpolation isn't the issue.   The quotes are needed to contain the variable as a single command line argument.  So that raises the question as to whether the Internal Field Separator (IFS) could be used to keep the entire line.separator definition as a single command line argument without quotes.</p>
  
  <p>Okay, it seems that if I add the following to the scala launch script, the line.separator setting seems to take effect:</p>
  
  <pre><code>IFS=' '
  </code></pre>
  
  <p>I can then append to JAVA_OPTS like this and get the desired behavior:</p>
  
  <pre><code>JAVA_OPTS="$JAVA_OPTS "-Dline.separator=$'\n'
  </code></pre>
  
  <p>The IFS setting has to occur prior to where the unquoted $JAVA_OPTS occurs.</p>
  
  <p>Update: this invocation, suggested by @som-snytt seems to work:</p>
  
  <pre><code>scala -J-Dline.separator=$'\n' -nc lineSeparatorBytes.sc
  </code></pre>