Search code examples
javabashshellenv

Bash stripping quotes - how to preserve quotes


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"

Solution

  • 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"'