how to pass line.separator to the JVM via bash variable JAVA_OPTS with no interpolation ; is it possible?











up vote
1
down vote

favorite












I'm hoping to solve a problem (described below) when setting environment variable JAVA_OPTS, which is the standard java (and scala) way to pass system properties to the JVM. I want to set line.separator to a single newline on Windows (it's already a single newline by default everywhere else).



BTW, some software (sbt, for example) will barf with this setting, but it's still useful, nevertheless.



This scala script is used to display the effective line.separator property:



#!/usr/bin/env scala
val bytes = sys.props("line.separator").map { _.toInt }.mkString(" ")
printf("line.separator bytes: %sn",bytes)


On a windows system, it normally prints the following:



line.separator bytes: 13 10


It does exactly what I want with the following JAVA_OPTS setting:



export JAVA_OPTS=-Dline.separator=$'n'


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):



execCommand 
"${JAVACMD:=java}"
$JAVA_OPTS
"${java_args[@]}"
"${classpath_args[@]}"
-Dscala.home="$SCALA_HOME"
$OVERRIDE_USEJAVACP
$WINDOWS_OPT
scala.tools.nsc.MainGenericRunner "$@"


With these two modifications, the test script above prints the following:



$ reportLineSeparator.sc
line.separator bytes: 10


Adding quotes to JAVA_OPTS is not a viable option, however, since that would prevent it from being unset, or from specifying multiple settings.



So the requirement seems to be to somehow arrange for the unquoted JAVA_OPTS to be correctly handled without losing the encoded newline.



I'm starting to suspect that there is solution, although I'm hopeful that someone will prove me wrong.



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:



"${JAVA_OPTS_ARR[@]}" 


I was pleasantly surprised that it also doesn't cause problems when JAVA_OPTS_ARR is undefined.



However, this isn't a viable solution because it's not possible to export bash arrays (see Exporting an array in bash script)



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.



Okay, it seems that if I add the following to the scala launch script, the line.separator setting seems to take effect:



IFS=' '


I can then append to JAVA_OPTS like this and get the desired behavior:



JAVA_OPTS="$JAVA_OPTS "-Dline.separator=$'n'


The IFS setting has to occur prior to where the unquoted $JAVA_OPTS occurs.










share|improve this question
























  • scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
    – som-snytt
    yesterday















up vote
1
down vote

favorite












I'm hoping to solve a problem (described below) when setting environment variable JAVA_OPTS, which is the standard java (and scala) way to pass system properties to the JVM. I want to set line.separator to a single newline on Windows (it's already a single newline by default everywhere else).



BTW, some software (sbt, for example) will barf with this setting, but it's still useful, nevertheless.



This scala script is used to display the effective line.separator property:



#!/usr/bin/env scala
val bytes = sys.props("line.separator").map { _.toInt }.mkString(" ")
printf("line.separator bytes: %sn",bytes)


On a windows system, it normally prints the following:



line.separator bytes: 13 10


It does exactly what I want with the following JAVA_OPTS setting:



export JAVA_OPTS=-Dline.separator=$'n'


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):



execCommand 
"${JAVACMD:=java}"
$JAVA_OPTS
"${java_args[@]}"
"${classpath_args[@]}"
-Dscala.home="$SCALA_HOME"
$OVERRIDE_USEJAVACP
$WINDOWS_OPT
scala.tools.nsc.MainGenericRunner "$@"


With these two modifications, the test script above prints the following:



$ reportLineSeparator.sc
line.separator bytes: 10


Adding quotes to JAVA_OPTS is not a viable option, however, since that would prevent it from being unset, or from specifying multiple settings.



So the requirement seems to be to somehow arrange for the unquoted JAVA_OPTS to be correctly handled without losing the encoded newline.



I'm starting to suspect that there is solution, although I'm hopeful that someone will prove me wrong.



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:



"${JAVA_OPTS_ARR[@]}" 


I was pleasantly surprised that it also doesn't cause problems when JAVA_OPTS_ARR is undefined.



However, this isn't a viable solution because it's not possible to export bash arrays (see Exporting an array in bash script)



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.



Okay, it seems that if I add the following to the scala launch script, the line.separator setting seems to take effect:



IFS=' '


I can then append to JAVA_OPTS like this and get the desired behavior:



JAVA_OPTS="$JAVA_OPTS "-Dline.separator=$'n'


The IFS setting has to occur prior to where the unquoted $JAVA_OPTS occurs.










share|improve this question
























  • scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
    – som-snytt
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm hoping to solve a problem (described below) when setting environment variable JAVA_OPTS, which is the standard java (and scala) way to pass system properties to the JVM. I want to set line.separator to a single newline on Windows (it's already a single newline by default everywhere else).



BTW, some software (sbt, for example) will barf with this setting, but it's still useful, nevertheless.



This scala script is used to display the effective line.separator property:



#!/usr/bin/env scala
val bytes = sys.props("line.separator").map { _.toInt }.mkString(" ")
printf("line.separator bytes: %sn",bytes)


On a windows system, it normally prints the following:



line.separator bytes: 13 10


It does exactly what I want with the following JAVA_OPTS setting:



export JAVA_OPTS=-Dline.separator=$'n'


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):



execCommand 
"${JAVACMD:=java}"
$JAVA_OPTS
"${java_args[@]}"
"${classpath_args[@]}"
-Dscala.home="$SCALA_HOME"
$OVERRIDE_USEJAVACP
$WINDOWS_OPT
scala.tools.nsc.MainGenericRunner "$@"


With these two modifications, the test script above prints the following:



$ reportLineSeparator.sc
line.separator bytes: 10


Adding quotes to JAVA_OPTS is not a viable option, however, since that would prevent it from being unset, or from specifying multiple settings.



So the requirement seems to be to somehow arrange for the unquoted JAVA_OPTS to be correctly handled without losing the encoded newline.



I'm starting to suspect that there is solution, although I'm hopeful that someone will prove me wrong.



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:



"${JAVA_OPTS_ARR[@]}" 


I was pleasantly surprised that it also doesn't cause problems when JAVA_OPTS_ARR is undefined.



However, this isn't a viable solution because it's not possible to export bash arrays (see Exporting an array in bash script)



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.



Okay, it seems that if I add the following to the scala launch script, the line.separator setting seems to take effect:



IFS=' '


I can then append to JAVA_OPTS like this and get the desired behavior:



JAVA_OPTS="$JAVA_OPTS "-Dline.separator=$'n'


The IFS setting has to occur prior to where the unquoted $JAVA_OPTS occurs.










share|improve this question















I'm hoping to solve a problem (described below) when setting environment variable JAVA_OPTS, which is the standard java (and scala) way to pass system properties to the JVM. I want to set line.separator to a single newline on Windows (it's already a single newline by default everywhere else).



BTW, some software (sbt, for example) will barf with this setting, but it's still useful, nevertheless.



This scala script is used to display the effective line.separator property:



#!/usr/bin/env scala
val bytes = sys.props("line.separator").map { _.toInt }.mkString(" ")
printf("line.separator bytes: %sn",bytes)


On a windows system, it normally prints the following:



line.separator bytes: 13 10


It does exactly what I want with the following JAVA_OPTS setting:



export JAVA_OPTS=-Dline.separator=$'n'


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):



execCommand 
"${JAVACMD:=java}"
$JAVA_OPTS
"${java_args[@]}"
"${classpath_args[@]}"
-Dscala.home="$SCALA_HOME"
$OVERRIDE_USEJAVACP
$WINDOWS_OPT
scala.tools.nsc.MainGenericRunner "$@"


With these two modifications, the test script above prints the following:



$ reportLineSeparator.sc
line.separator bytes: 10


Adding quotes to JAVA_OPTS is not a viable option, however, since that would prevent it from being unset, or from specifying multiple settings.



So the requirement seems to be to somehow arrange for the unquoted JAVA_OPTS to be correctly handled without losing the encoded newline.



I'm starting to suspect that there is solution, although I'm hopeful that someone will prove me wrong.



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:



"${JAVA_OPTS_ARR[@]}" 


I was pleasantly surprised that it also doesn't cause problems when JAVA_OPTS_ARR is undefined.



However, this isn't a viable solution because it's not possible to export bash arrays (see Exporting an array in bash script)



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.



Okay, it seems that if I add the following to the scala launch script, the line.separator setting seems to take effect:



IFS=' '


I can then append to JAVA_OPTS like this and get the desired behavior:



JAVA_OPTS="$JAVA_OPTS "-Dline.separator=$'n'


The IFS setting has to occur prior to where the unquoted $JAVA_OPTS occurs.







bash scala interpolation java-opts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









philwalk

4761511




4761511












  • scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
    – som-snytt
    yesterday


















  • scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
    – som-snytt
    yesterday
















scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
– som-snytt
yesterday




scala -J-Dline.separator=$'r'$'n' seems to work because of java_args.
– som-snytt
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













JAVA_OPTS is an ancient convention but not a standard. It was introduced to the scala script in 2007 and overtaken by -J in 2010.



I think the best option (so to speak) is scala -J-Dline.separator=$'r'$'n'.



There is a PR now for your suggestion, which seems safe, except it also keeps tab for the case:



JAVA_OPTS="-Xmx256m <tab> -Xss1m".



Shell quoting is so much fun! I try to refresh my scarred memory every five years or so.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343645%2fhow-to-pass-line-separator-to-the-jvm-via-bash-variable-java-opts-with-no-interp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    JAVA_OPTS is an ancient convention but not a standard. It was introduced to the scala script in 2007 and overtaken by -J in 2010.



    I think the best option (so to speak) is scala -J-Dline.separator=$'r'$'n'.



    There is a PR now for your suggestion, which seems safe, except it also keeps tab for the case:



    JAVA_OPTS="-Xmx256m <tab> -Xss1m".



    Shell quoting is so much fun! I try to refresh my scarred memory every five years or so.






    share|improve this answer



























      up vote
      0
      down vote













      JAVA_OPTS is an ancient convention but not a standard. It was introduced to the scala script in 2007 and overtaken by -J in 2010.



      I think the best option (so to speak) is scala -J-Dline.separator=$'r'$'n'.



      There is a PR now for your suggestion, which seems safe, except it also keeps tab for the case:



      JAVA_OPTS="-Xmx256m <tab> -Xss1m".



      Shell quoting is so much fun! I try to refresh my scarred memory every five years or so.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        JAVA_OPTS is an ancient convention but not a standard. It was introduced to the scala script in 2007 and overtaken by -J in 2010.



        I think the best option (so to speak) is scala -J-Dline.separator=$'r'$'n'.



        There is a PR now for your suggestion, which seems safe, except it also keeps tab for the case:



        JAVA_OPTS="-Xmx256m <tab> -Xss1m".



        Shell quoting is so much fun! I try to refresh my scarred memory every five years or so.






        share|improve this answer














        JAVA_OPTS is an ancient convention but not a standard. It was introduced to the scala script in 2007 and overtaken by -J in 2010.



        I think the best option (so to speak) is scala -J-Dline.separator=$'r'$'n'.



        There is a PR now for your suggestion, which seems safe, except it also keeps tab for the case:



        JAVA_OPTS="-Xmx256m <tab> -Xss1m".



        Shell quoting is so much fun! I try to refresh my scarred memory every five years or so.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 21 hours ago

























        answered 23 hours ago









        som-snytt

        34.2k23098




        34.2k23098






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343645%2fhow-to-pass-line-separator-to-the-jvm-via-bash-variable-java-opts-with-no-interp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?