Escape double quotes in a Jenkins pipeline file's shell command
33,988
Solution 1
To expand on my comment, a quick test revealed its the case.
You need to escape twice, once the quote for the shell with a slash, and once that slash with a slash for groovy itself.
node() {
sh 'echo "asdf"'
sh 'echo \"asdf\"'
sh 'echo \\"asdf\\"'
}
Result
[Pipeline] {
[Pipeline] sh
+ echo asdf
asdf
[Pipeline] sh
+ echo asdf
asdf
[Pipeline] sh
+ echo "asdf"
"asdf"
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Solution 2
After long time of struggling and googling, this is what has worked for me on similar use case:
sh("ssh [email protected] \"su user -c \\\"mkdir ${newDirName}\\\"\"")
Solution 3
I had double quotes inside the variable, so escaped single quotes worked for me:
sh "git commit -m \'${ThatMayContainDoubleQuotes}\'"
Author by
Archit Arora
Updated on February 05, 2022Comments
-
Archit Arora about 2 years
Below is a snippet from my Jenkins file -
stage('Configure replication agents') { environment { AUTHOR_NAME="XX.XX.XX.XX" PUBLISHER_NAME="XX.XX.XX.XX" REPL_USER="USER" REPL_PASSWORD="PASSWORD" AUTHOR_PORT="4502" PUBLISHER_PORT="4503" AUTHOR="http://${AUTHOR_NAME}:${AUTHOR_PORT}" PUBLISHER="http://${PUBLISHER_NAME}:${PUBLISHER_PORT}" S_URI= "${PUBLISHER}/bin/receive?sling:authRequestLogin=1" } steps { sh 'curl -u XX:XX --data "status=browser&cmd=createPage&label=${PUBLISHER_NAME}&title=${PUBLISHER_NAME}&parentPath =/etc/replication/agents.author&template=/libs/cq/replication/templates/agent" ${AUTHOR}/bin/wcmcommand' }
The above command, in Jenkins console, is printed as
curl -u XX:XX --data status=browser&cmd=createPage&label=XXXX&title=XXX&parentPath =/etc/replication/agents.author&template=/libs/cq/replication/templates/agent http://5XXXX:4502/bin/wcmcommand
Note how the double quotes
""
are missing.I need to preserve the double quotes after
--data
in this command. How do I do it? I tried using forward slashes but that didnt work.Cheers
-
Iorek about 4 years
sh 'echo \\"asdf\\"'
was the solution for me, since I could not use single quotes to wrap\\"asdf\\"