1

I'm running a command on my client which sends information to my server:

bin/script.sh ack --id=10 --reason="This is the reason I ack"

This sends a message to a server but for the reason field, it fills in:

reason = "This

I've tried to escape the space (and the quote):

--reason=\"This\ is..."

But I still can't escape the space. The above gets recorded :

reason = "This

Inside of script.sh there is a line:

#!/bin/sh

RUNDIR=`dirname $0`/..
HQAPILOGDIR=$RUNDIR/logs

CLASSPATH=$CLASSPATH:$RUNDIR/conf

for h in `ls $RUNDIR/*.jar`; do
   CLASSPATH=$CLASSPATH:$h
done

for i in `ls $RUNDIR/lib/*.jar`; do
   CLASSPATH=$CLASSPATH:$i
done

java -Dhqapi.logDir=$HQAPILOGDIR -cp $CLASSPATH org.hyperic.hq.hqapi1.tools.Shell "$@"

From what I understand, the $@ symbol brings in the arguments from the command line so I'm guessing that this is where my problem is. Is there a way to escape the spaces in my command line arguments? Is the $@ where my problem is occurring?

14
  • 6
    Using "$@" already passes spaces exactly as-given. No other escaping need be used. If the code were $@, then it would have the behavior you describe. Oct 1, 2014 at 13:49
  • 1
    ...as such, because you aren't providing enough information to reproduce the problem, I don't know that we can help you. Oct 1, 2014 at 13:51
  • 3
    @mike, an old answer of mine illustrates $@ vs "$@": stackoverflow.com/questions/12314451/… Oct 1, 2014 at 13:51
  • although this might help: --reason="\"This is the reason\""
    – isedev
    Oct 1, 2014 at 13:52
  • 2
    @isedev, if that helps, the code is doing something very broken (particularly, an eval pass). It should not be suggested as a solution; instead, whatever code earlier in the file (and not given in the question) is passing the arguments through eval should be fixed. Oct 1, 2014 at 13:53

1 Answer 1

3

"$@" does keep arguments together, without putting them through string-splitting.

You can trivially test this yourself:

#!/bin/sh
printf '%s\n' "$@"

Each argument will be printed on its own line, even if that argument contains spaces.


By the way -- don't escape the quotes! If you write \"This, then the quote is treated as data, which makes it no longer syntax, which means it no longer functions as a quote at all.


Here's a fully cleaned-up copy of your script:

#!/bin/sh

# these aren't environment variables and should be lower-case
rundir=$(dirname "$0")/..
hqapilogdir=$rundir/logs

for h in "$rundir/conf" "$rundir/"*.jar "$rundir/lib/"*.jar; do
   [ -e "$h" ] && CLASSPATH=$CLASSPATH:$h
done

# trim any leading colon from the generated CLASSPATH
CLASSPATH=${CLASSPATH#:}

exec java \
  -Dhqapi.logDir="$hqapilogdir" \
  -cp "$CLASSPATH" \
  org.hyperic.hq.hqapi1.tools.Shell "$@"

If arguments are still parsed incorrectly, the problem is inside the Java code being invoked, not the shell wrapping it. You can verify this by running your script with sh -x scriptname ack --id=10 --reason="This is the reason I ack", and watching the commands invoked below.

2
  • Thanks for the clean up, I'll try this out and see what happens though I suspect the problem is in Java. +1
    – Mike
    Oct 3, 2014 at 17:44
  • I still suggest using (and perhaps posting the output of, if you have any questions) sh -x yourscript. Oct 3, 2014 at 17:46

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.