简体   繁体   中英

Appending multiple commands to java process with environment variable in bash

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.

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM