Bash stripping quotes - how to preserve quotes

2.6k views Asked by At

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"
2

There are 2 answers

2
b4hand On BEST ANSWER

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"'
3
user3213851 On

You can pass argument like this

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