Issue
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.
Solution
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
)
Answered By - John Kugelman
Answer Checked By - Marilyn (JavaFixing Volunteer)