Bash inserting additional, invalid single quotes when quoting an argument list

I am trying to create a program that will start Maven with certain arguments.

This is mainly done:

ARGUMENTS=\""${@:2}"\"
mvn exec:java -Dexec.mainClass=$1 -Dexec.args=$ARGUMENTS

Thus, a launch ./myScript.sh a b cshould result in a launch:

mvn exec:java -Dexec.mainClass=a -Dexec.args="b c"

But Maven throws errors about an unknown life cycle. The face-off set -xthere tells me that the team actually turns into:

mvn exec:java -Dexec.mainClass=a '-Dexec.args="b' 'c"'

Echoing $ARGUMENTSgives the expected "b c". What causes the addition of these extra quotes and how can I fix this to get my intended results?

+4
source share
2 answers
arguments="${*:2}"
mvn exec:java -Dexec.mainClass="$1" -Dexec.args="$arguments"

See BashFAQ # 50 for a full explanation. Nonetheless:

"$@" argv - , --foo="$@", set -- hello world, "--foo=hello" "world".

"$*" argv IFS , .

$*, , , glob, , ( ) - , .

arguments="\"${*:2}\"" - "hello world". -Dexec.args=$arguments, ( , glob):

  • . , , . -Dexec.args=$arguments , .
  • . -Dexec.args="hello world"
  • . , , . , -Dexec.args="hello , world" .

, -Dexec.args="$arguments" ( , "-Dexec.args=$arguments"). :

  • . $arguments .
  • . arguments, , -Dexec.args="hello world" , Maven, .
  • . , , .

, Maven ( !), , .

+5

, . BASH:

arr=( "$@" )
mvn exec:java -Dexec.mainClass="$1" -Dexec.args="${arr[*]:1}"
+3

Source: https://habr.com/ru/post/1548392/


All Articles