0

I have a command I want to run:

sbt "testOnly com.example.testClass"

which needs to be ran with the quotes. However, what I really want to do is be able to pass the argument in a variable, while keeping the quotes.

This does not work:

TEST_CMD="\"testOnly com.example.testClass\""
sbt $TEST_CMD

This does work:

TEST_CMD="\"testOnly com.example.testClass\""
eval sbt $TEST_CMD

I read http://mywiki.wooledge.org/BashFAQ/050 and now I understand why the first doesn't work, and I've also learned that eval can be insecure and should be avoided (this is just an internal Jenkins job, would it ever be an issue?).

Also in the article, it mentioned adding the command to an array first, so I tried:

 args=("\"testOnly com.example.testClass\"")
 sbt "${args[@]}"

but that also does not run correctly. What's the best way to do this? Is it really that bad to use eval in my case?

Davis Mariotti
  • 574
  • 1
  • 4
  • 23

1 Answers1

1

Quote the variable expansion rather than the assignment.

TEST_CMD="testOnly com.example.testClass"
sbt "$TEST_CMD"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578