Skip to content

Instantly share code, notes, and snippets.

@cyrille-leclerc
Last active March 9, 2022 23:41
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cyrille-leclerc/8cad9d1b35ea553820a1 to your computer and use it in GitHub Desktop.
Save cyrille-leclerc/8cad9d1b35ea553820a1 to your computer and use it in GitHub Desktop.
Escape character & quotes in Jenkins Pipeline
docker.image('cloudbees/java-build-tools:0.0.6').inside {
sshagent(['github-ssh-credentials']) {
sh """
git version
git config --local user.email \\"cleclerc@cloudbees.com\\"
git config --local user.name \\"Cyrille Le Clerc\\"
git clone git@github.com:cyrille-leclerc/a-test-repo.git
date &> now.txt
git commit --all -m \\"another commit\\"
git push origin/master
"""
}
}
Started by user admin
[Pipeline] Allocate node : Start
Running on docker-agent.beesshop.org in /home/ubuntu/jenkins-aws-home/workspace/tests/test-git
[Pipeline] node {
[Pipeline] sh
[test-git] Running shell script
+ docker inspect -f . cloudbees/java-build-tools:0.0.6
.
[Pipeline] Run build steps inside a Docker container : Start
$ docker run -t -d -u 1000:1000 -w /home/ubuntu/jenkins-aws-home/workspace/tests/test-git -v /home/ubuntu/jenkins-aws-home/workspace/tests/test-git:/home/ubuntu/jenkins-aws-home/workspace/tests/test-git:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** cloudbees/java-build-tools:0.0.6 cat
[Pipeline] withDockerContainer {
[Pipeline] SSH Agent : Start
[ssh-agent] Using credentials cyrille-leclerc (GitHub cyrille-leclerc ssh)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent] Java/JNR ssh-agent
[ssh-agent] Started.
[Pipeline] sshagent {
[Pipeline] sh
[test-git] Running shell script
+ git version
git version 2.1.4
+ git config --local user.email "cleclerc@cloudbees.com"
+ git config --local user.name "Cyrille Le Clerc"
usage: git config [options]
Config file location
--global use global config file
--system use system config file
--local use repository config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object
Action
--get get value: name [value-regex]
--get-all get all values: key [value-regex]
--get-regexp get values for regexp: name-regex [value-regex]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value_regex]
--add add a new variable: name value
--unset remove a variable: name [value-regex]
--unset-all remove all matches: name [value-regex]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
-e, --edit open an editor
--get-color <slot> find the color configured: [default]
--get-colorbool <slot>
find the color setting: [stdout-is-tty]
Type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--path value is a path (file or directory name)
Other
-z, --null terminate values with NUL byte
--includes respect include directives on lookup
[Pipeline] } //sshagent
[Pipeline] SSH Agent : End
[Pipeline] } //withDockerContainer
$ docker stop 6a8ba100abb54669d24cfc7d4b6cae0dfaad727b8f3c7a3ad186690d9ee12d4a
$ docker rm -f 6a8ba100abb54669d24cfc7d4b6cae0dfaad727b8f3c7a3ad186690d9ee12d4a
[Pipeline] Run build steps inside a Docker container : End
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
ERROR: script returned exit code 129
Finished: FAILURE
@imavroukakis
Copy link

imavroukakis commented Sep 29, 2016

i.e. git config --local user.name \\"Cyrille\\ Le\\ Clerc\\"

@bcatubig
Copy link

bcatubig commented Jan 4, 2017

Just ran into this issue myself.

If it's not clear how to do this, try with 3 single quotes and using double backslashed double quotes

Example

sh '''
    echo Hello \\"World\\"
    echo This is a dollar sign: \\$
'''

@chee
Copy link

chee commented Jan 10, 2017

surely three single quotes is just the same as one single quote. you're just joining the empty string to either end of it

@vikas027
Copy link

vikas027 commented May 1, 2017

Life saver 💯 👍

@Yograj13
Copy link

how to use wild character and variable together in double quoted string in jenkins pipeline
I tried
def var=1.0.4
"mv job*.war job-${var}.war"
wild character not working for me. can someone help.

@TiloHeidasch
Copy link

Just ran into this issue myself.

If it's not clear how to do this, try with 3 single quotes and using double backslashed double quotes

Example

sh '''
    echo Hello \\"World\\"
    echo This is a dollar sign: \\$
'''

This worked for me, thanks for the hint!
sh label: 'prep', script: '''echo {\"results\": > results.json'''

@xserrat
Copy link

xserrat commented Jan 28, 2020

It works for me!

String escaped_commit_message = commit_message.replace('"', '\\"')
escaped_commit_message = escaped_commit_message.replace("'", /'"'"'/)

@emranbm
Copy link

emranbm commented Sep 27, 2020

It works for me!

String escaped_commit_message = commit_message.replace('"', '\\"')
escaped_commit_message = escaped_commit_message.replace("'", /'"'"'/)

Nice! Thank you so much.
Would you please explain the second (slashy) replacement? I'm new to groovy and have read a little about that syntax, but don't understand why/how '"'"' works?

@needleshaped
Copy link

needleshaped commented Oct 13, 2021

Unless you must, best way is to use combination of single and double quotes, e.g.:

        sh """
            git config --local user.email \\"cleclerc@cloudbees.com\\"
            git config --local user.email 'cleclerc@cloudbees.com'

Whether you must or not depends on the use of variables (groovy or shell).

P.S. very good behavioral examples: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 (named "Jenkinsfile idiosynchrasies with escaping and quotes" ;) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment