Home > Net >  Shell script syntax - adding quote to end of line
Shell script syntax - adding quote to end of line

Time:12-23

I have below script which gives me syntax error if I add the single quote at the end of the line where I am assigning the java vm arguments. Why this syntax is wrong?

#!/bin/bash

#Incorrect

JVM_OPTS='-XX: UseG1GC -Xms250M -Xmx250M -Xss1M  -DVAR1='"$VALUE1"' -DVAR2='"$VALUE2"'
START_CMD="java ${JVM_OPTS} ${JVM_ARGS} -jar ${1}"
$START_CMD

#!/bin/bash

#Correct

JVM_OPTS='-XX: UseG1GC -Xms250M -Xmx250M -Xss1M  -DVAR1='"$VALUE1"' -DVAR2='"$VALUE2"
START_CMD="java ${JVM_OPTS} ${JVM_ARGS} -jar ${1}"
$START_CMD

CodePudding user response:

You should be using an array (and/or a function)

JVM_OPTS=(-XX: UseG1GC -Xms250M -Xmx250M -Xss1M  -DVAR1="$VALUE1" -DVAR2="$VALUE2")
JVM_ARGS=(...)

start_cmd () {
    java "${JVM_OPTS[@]}" "${JVM_ARGS[@]}" -jar "$1"
}

start_cmd "$1"

CodePudding user response:

Let's match the start and end quotes:

JVM_OPTS='-XX: UseG1GC -Xms250M -Xmx250M -Xss1M  -DVAR1='"$VALUE1"' -DVAR2='"$VALUE2"'
#        \............................................../\......./\......../\......./^

You have an unmatched quote.

But use @chepner's suggestions. Building up a command into a single string is bound to fail.

  • Related