1

I have a bash shell which I need to modify, and I have to set a variable in a script and then call another script. My variable, EXTRA_JAVA_OPTIONS must be

-javaagent:myagent.jar="-d 0 -i 1000 -l log2 -c log1"

So what I did is

export EXTRA_JAVA_OPTIONS="-javaagent:myagent.jar=\"-d 0 -i 1000 -l log2 -c log1 \""

Then I call my script and inside my script there is the following :

"$JAVACMD" "$EXTRA_JAVA_OPTIONS"

However, this won't produce the result I desire. Running bash -x I can see that

+ /usr/java/jdk1.6.0_38/bin/java '-javaagent:myagent.jar="-d 0 -i 1000 -l log2 -c log1"'

If on the contrary I do:

"$JAVACMD" $EXTRA_JAVA_OPTIONS

I got a different version of the output, which is wrong too

+ /usr/java/jdk1.6.0_38/bin/java '-javaagent:myagent.jar="-d' 0 -i 1000 -l log2 -c 'log1"'

How do I prevent ' from appearing when printing my strings, or them to be added by the shell?

2
  • Weird. What does "$JAVACMD $EXTRA_JAVA_OPTIONS" do? Or just plain no quotes at all?
    – goldilocks
    Jan 11 '13 at 16:05
  • Did you mean to pass a single argument to java containing among others two " characters? You show several things that go wrong, but what is your desired result (making sure to distinguish between any quoting that you're using for presentation, and quotes and whitespace that must end up in an argument passed to java)? Jan 11 '13 at 23:43
3

The quotes are just to prevent splitting by word. But you already have quotes around the whole thing.

export EXTRA_JAVA_OPTIONS="-javaagent:myagent.jar=-d 0 -i 1000 -l log2 -c log1"
3

Using an array is a safe way to keep your arguments containing whitespace intact:

EXTRA_JAVA_OPTIONS=(-javaagent:myagent.jar="-d 0 -i 1000 -l log2 -c log1")
"$JAVACMD" "${EXTRA_JAVA_OPTIONS[@]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.