Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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)? –  Gilles Jan 11 '13 at 23:43

2 Answers 2

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"
share|improve this answer

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[@]}"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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