1

I have a java process and I'm trying to append multiple parameters to it loaded from an environment variable in a bash file. Example:

export JAVA_OPTS="-Dparam.one.name=value_one -Dparam.two.name=value_two"

#not working:
java \
  "${JAVA_OPTS}" \
  -jar cookie.jar

#working:
java \
  -Dparam.one.name=value_one \
  -Dparam.two.name=value_two \
  -jar cookie.jar

Is there a way to get around this? Is this a formatting problem? The idea behind this is that I don't want to change the script if I want to use some other/new parameter, I only want to change the environment variable.

Maybe it would work if I put everything in one line, but that would make the actual command extremely long, so that is not an optimal solution.

Also the same thing is working perfectly with one property.

I really appreciate other approaches as well.

1 Answer 1

1

Quoting variables means they're expanded to a single word. Normally that's a good thing, but in this case you want word splitting, so don't quote the expansion.

It's also a good idea to disable glob expansion in case $JAVA_OPTS contains any glob characters like * or ?. You don't want those to be accidentally expanded. Since that's a global setting that could negatively affect other commands I recommend doing it inside a subshell so the change is limited to the java command.

(
  set -o noglob
  java \
    $JAVA_OPTS \
    -jar cookie.jar
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.