I can write the command
exec sleep '5'
and it works just fine.
But say I have a file called cmd.txt containing the following string
sleep '5'
I want to read this into a variable, then I want to exec this command.
cmd=$(cat cmd.txt)
exec $cmd
Then this fails with.
+ exec sleep ''\''5'\'''
sleep: invalid time interval ‘'5'’
Try 'sleep --help' for more information.
The sleep command is a trivial example to demonstrate the problem, the real command is a lengthy java command also with some single quotes in it. It needs to be exec and not eval as we need to replace the current process.
A workaround involves creating a file with "exec " followed by the command and running.
. file
But I would like to avoid creating a file just to do this.
The full command is, and is generated by some other code
/opt/java/jdk-17.0.6+10/bin/java -Djava.io.tmpdir=/tmp -Djetty.home=/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home -Djetty.base=/tmp/jetty-test --class-path /tmp/jetty-test/resources:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/logging/slf4j-api-2.0.5.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/logging/jetty-slf4j-impl-10.0.15-SNAPSHOT.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-servlet-api-4.0.6.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-http-10.0.15-SNAPSHOT.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-server-10.0.15-SNAPSHOT.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-xml-10.0.15-SNAPSHOT.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-util-10.0.15-SNAPSHOT.jar:/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/jetty-io-10.0.15-SNAPSHOT.jar org.eclipse.jetty.xml.XmlConfiguration 'java.version'='17.0.6' 'java.version.major'='17' 'java.version.micro'='6' 'java.version.minor'='0' 'java.version.platform'='17' 'jetty.base'='/tmp/jetty-test' 'jetty.base.uri'='file:///tmp/jetty-test' 'jetty.home'='/home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home' 'jetty.home.uri'='file:///home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home' 'jetty.http.port'='8181' 'jetty.webapp.addServerClasses'='org.eclipse.jetty.logging.,file:///home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/lib/logging/,org.slf4j.' 'runtime.feature.alpn'='true' 'slf4j.version'='2.0.5' /home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/etc/jetty-bytebufferpool.xml /home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/etc/jetty-threadpool.xml /home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/etc/jetty.xml /home/lachlan/webtide/jetty-10.0.x/jetty-home/target/jetty-home/etc/jetty-http.xml