Bash stripping quotes - how to preserve quotes [duplicate]
Asked Answered
B

2

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"
Brownie answered 10/11, 2014 at 13:47 Comment(7)
$> ./test.sh -t some.comman -extraswitch '"some addtional info"' should do itMarvismarwin
"$@" will properly pass the arguments to java. In what way is your java program failing to handle its arguments?Pros
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.Plautus
Use a backslash "\"stuff\""Deboradeborah
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 $@.Perez
Why do you want to preserve the quotes? The command line will properly arrange the arguments in your array; what code fails without them?Brewington
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.Brownie
R
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"'
Rhinehart answered 11/11, 2014 at 8:25 Comment(2)
I would rather have my script take care of the quote instead of me changing all the crontab entries.Brownie
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.Rhinehart
A
-1

You can pass argument like this

./test.sh -t some.comman -extraswitch \"some addtional info\"
Antipodal answered 10/11, 2014 at 17:35 Comment(3)
This is definitely a bad answer: test.sh will see 6 arguments: -t and some.comman and -extraswitch and "some and addtional and info".Luciennelucier
You should edit your answer to be: ./test.sh -t some.comman -extraswitch "\"some addtional info\""Overflow
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.Brownie

© 2022 - 2024 — McMap. All rights reserved.