'Escape double quotes in a Jenkins pipeline file's shell command

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



Solution 1:[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:[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:[3]

I had double quotes inside the variable, so escaped single quotes worked for me:

sh "git commit -m \'${ThatMayContainDoubleQuotes}\'"

Solution 4:[4]

I needed the output to be with trailing \\ so I had to do something like this

echo 'key1 = \\\\"__value1__\\\\"' > auto.file
File looks like
cat auto.file
key1 = \\"__value1__\\"
Dependent Script
            export value1="some-value"
            var=${value1}

            # Read in template one line at the time, and replace variables
            tmpfile=$(mktemp)
            sed -E 's/__(([^_]|_[^_])*)__/${\\1}/g' auto.file > ${tmpfile}

            while read auto
            do
              eval echo "$auto"
            done < "${tmpfile}" > autoRendered.file

            rm -f ${tmpfile}
Rendered File looks like
cat autoRendered.file
key1 = "some-value"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominik Gebhart
Solution 2 teejay
Solution 3 Alexander
Solution 4