1

I need to pass cmd parameters to my bash script, but it keeps on stripping out the quotes.

Below is an example. In my script, I add some additional processing where I add data to my first variable before I process the command

$> ./test.sh -t some.comman -extraswitch "some addtional info"

The script uses Java to do some processing, but the "some additional info" is missing the quotes and thus can't execute the Java portion of this script. How can I preserve the quotes from the command line so that I can execute my Java command in my script?

Java command in script

java $JAVA_OPTS "$@"

Output

java lib -someoption -anotheroption -javalibswitch some additional info 

Intended Output

java lib -someoption -anotheroption -javalibswitch "some additional info"
Kendra
  • 769
  • 1
  • 20
  • 34
user3914315
  • 29
  • 1
  • 3
  • 2
    `$> ./test.sh -t some.comman -extraswitch '"some addtional info"'` should do it –  Nov 10 '14 at 13:48
  • 5
    `"$@"` *will* properly pass the arguments to java. In what way is your java program failing to handle its arguments? – glenn jackman Nov 10 '14 at 13:59
  • 1
    Please share exactly what you are doing in the script. If you're playing around with the parameters or using a shell function definition, the above command will behave differently. – RealSkeptic Nov 10 '14 at 14:10
  • Use a backslash `"\"stuff\""` –  Nov 10 '14 at 14:33
  • 2
    Since the invocation you show does not match the `java` command line you claim doesn't work, I think your problem is with `JAVA_OPTS`, no `$@`. – chepner Nov 10 '14 at 14:55
  • Why do you want to preserve the quotes? The command line will properly arrange the arguments in your array; what code fails without them? – jpmc26 Nov 10 '14 at 17:37
  • I have to call this script via crontab. I have multiple crontab entries that look like my example where i have to pass the full cmd string to java to execut. Im trying to avoid having to hard escape the quotes i.e. "\" on each crontab entry as i would like a more generic way of passing the value including the quotes to java. The quoted portion of this is mostly additional sql that is processed by java and that is why i need to pass the quotes else we will have to have massive rework of our standalone java jobs. – user3914315 Nov 12 '14 at 09:45

2 Answers2

3

I typically use single quotes to avoid shell interpolation when you want to quote strings with quotes in them.

./test.sh -t some.comman -extraswitch '"some addtional info"'
b4hand
  • 9,550
  • 4
  • 44
  • 49
  • I would rather have my script take care of the quote instead of me changing all the crontab entries. – user3914315 Nov 12 '14 at 09:50
  • 1
    It doesn't work that way. The shell strips those quotes before they ever get to the process in which your script is running. If you want a literal quote passed to the script, you must quote the quote. – b4hand Nov 12 '14 at 17:20
-1

You can pass argument like this

./test.sh -t some.comman -extraswitch \"some addtional info\"
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
user3213851
  • 1,068
  • 2
  • 12
  • 24
  • 2
    This is definitely a bad answer: `test.sh` will see 6 arguments: `-t` and `some.comman` and `-extraswitch` and `"some` and `addtional` and `info"`. – gniourf_gniourf Nov 10 '14 at 20:03
  • 2
    You should edit your answer to be: `./test.sh -t some.comman -extraswitch "\"some addtional info\""` – Jimothy Nov 10 '14 at 20:15
  • I have to call this script via crontab. I have multiple crontab entries that look like my example where i have to pass the full cmd string to java to exeute. Im trying to avoid having to hard escape the quotes i.e. "\" on each crontab entry as i would like a more generic way of passing the value including the quotes to java. The quoted portion of this is mostly additional sql that is processed by java and that is why i need to pass the quotes else we will have to have massive rework of our standalone java jobs. – user3914315 Nov 12 '14 at 09:47