Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkinsfile idiosynchrasies with escaping and quotes
node {
echo 'Results included as an inline comment exactly how they are returned as of Jenkins 2.121, with $BUILD_NUMBER = 1'
echo 'No quotes, pipeline command in single quotes'
sh 'echo $BUILD_NUMBER' // 1
echo 'Double quotes are silently dropped'
sh 'echo "$BUILD_NUMBER"' // 1
echo 'Even escaped with a single backslash they are dropped'
sh 'echo \"$BUILD_NUMBER\"' // 1
echo 'Using two backslashes, the quotes are preserved'
sh 'echo \\"$BUILD_NUMBER\\"' // "1"
echo 'Using three backslashes still results in only preserving the quotes'
sh 'echo \\\"$BUILD_NUMBER\\\"' // "1"
echo 'To end up with \" use \\\\\\" (yes, six backslashes)'
sh 'echo \\\\\\"$BUILD_NUMBER\\\\\\"'
echo 'This is fine and all, but we cannot substitute Jenkins variables in single quote strings'
def foo = 'bar'
sh 'echo "${foo}"' // (returns nothing)
echo 'This does not interpolate the string but instead tries to look up "foo" on the command line, so use double quotes'
sh "echo \"${foo}\"" // bar
echo 'Great, more escaping is needed now. How about just concatenate the strings? Well that gets kind of ugly'
sh 'echo \\\\\\"' + foo + '\\\\\\"' // \"bar\"
echo 'We still needed all of that escaping and mixing concatenation is hideous!'
echo 'There must be a better way, enter dollar slashy strings (actual term)'
def command = $/echo \\\"${foo}\\\"/$
sh command // \"bar\"
echo 'String interpolation works out of the box as well as environment variables, escaped with double dollars'
def vash = $/echo \\\"$$BUILD_NUMBER\\\" ${foo}/$
sh vash // \"3\" bar
echo 'It still requires escaping the escape but that is just bash being bash at that point'
echo 'Slashy strings are the closest to raw shell input with Jenkins, although the non dollar variant seems to give an error but the dollar slash works fine'
}
@ntwrkguru
Copy link

ntwrkguru commented Apr 22, 2020

This is amazing! Jenkins is not!

Oh, don't be too harsh. It's still the best at what it does, even with it's nuances!

@fortil
Copy link

fortil commented May 15, 2020

Hello everyone,
this is an amazing gist... ...thank you for all.

I have a question, how can I run sh script into the console script tool?
thank you!

@VGerris
Copy link

VGerris commented Sep 7, 2020

Thank you for putting this list together.. spent most of the day trying to figure out a simple sh command with a sed replacement ..

same here, three years later :). This seems to apply for the secret text storage in the general settings. To have sed get a backslash in front of a forward slash, I needed to add the 6 backslashes in the secret string to have one in the eventual variable. Works though, wonder how long it would have taken to foind that without the post here.

@DamianFekete
Copy link

DamianFekete commented Oct 21, 2020

Thanks... :|

@Rajdeep-689
Copy link

Rajdeep-689 commented Oct 31, 2020

Hey Guys. I have one powershell command inside that both Double quotes and single quotes are present and it's needed for sure in order to make the command execute successfully in dos prompt. I am not sure how to make it escape in pipeline script.

bat "powershell -Command "(Get-Content "${Physical_FolderLoc}\${Stord_Process_Name}.txt") | ForEach-Object { $_ -replace [RegEx]::Escape('E:\config'), 'I:\config' } | Set-Content "${Physical_FolderLoc}\${Stord_Process_Name}.txt" " "

In the above command you can see the second last " is ending quote for Get Content and last one is for bat command.
I tried the above command with triple slash but getting groovy error.
groovy.lang.MissingPropertyException: No such property: _ for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty

Please help. Thanks
@haridsv @shadycuz @agray

@Rajdeep-689
Copy link

Rajdeep-689 commented Nov 2, 2020

Anyone who can address my concern please!!! I am seeking an expert advice. Please help.

@fortil
Copy link

fortil commented Nov 2, 2020

Anyone who can address my concern please!!! I am seeking an expert advice. Please help.

check the example and you will know how to do it, you're bad with the quotes, you have to scape them

@Rajdeep-689
Copy link

Rajdeep-689 commented Nov 3, 2020

@fortil I know I'm bad at the quotes that's why asked in the forum, so that someone can share some knowledge or one sample... So next time onwards I might not ask the same question trust me.. I checked the example and tried all possible ways (triple slash \\ or single quote ' or slash" slash")what I understood, but it's not happening. I tried base64 string encoding as well, but in that the Jenkins params are not resolving. Yeah if we take any hardcoded folder location or file name in the powershell command then that's a really good solution. Anyways thanks for your time, if I will be able to resolve it then definitely share the sample with you..

@shadycuz
Copy link

shadycuz commented Nov 3, 2020

@Rajdeep-689

I dont have access to a windows node to test this, but in general you can think of your command like this...

"power shell command "inner command "quoted parameter"""

Try not too nest so many of the same quotes together.

'power shell command "inner command \'quoted parameter\'"'

Something sorta like this except I don't remember how to escape on windows 😊

Stack overflow devops would be a decent place to ask this question.

Also if Physical_FolderLoc and Stord_Process_Name are not groovy variables then they need escaped as well.

@Rajdeep-689
Copy link

Rajdeep-689 commented Nov 3, 2020

@shadycuz @fortil @haridsv @shadycuz @agray

I got the exact issue why the command was not escaping. There is a stand alone $ symbol in the powershell command which is also a system character for groovy, so that need to be escaped as well.(The $ character which is after ForEach-Object {). It's now prefixed with \, so escaped.

Now the complete command is :
bat "powershell -Command \"(Get-Content ${Physical_FolderLoc}\\${Stord_Process_Name}.txt) | ForEach-Object { \$_ -replace [RegEx]::Escape('E:\\config'), 'I:\\config' } | Set-Content ${Physical_FolderLoc}\\${Stord_Process_Name}.txt\" "

Thanks a lot for the suggestion and thanks everyone for your time.

@carpiupin
Copy link

carpiupin commented Jan 20, 2021

I'm trying to execute the following maven command with jenkins sh:
mvn release:perform -Darguments="-Dmaven.javadoc.skip=true -DskipTests"

But jenkins always introduce random single quotes and it doesn't work...

Ex1: sh 'mvn release:perform -Darguments="-Dmaven.javadoc.skip=true -DskipTests"' // Run: mvn release:perform '-Darguments=-Dmaven.javadoc.skip=true -DskipTests'
Ex2: sh 'mvn release:perform -Darguments=\\"-Dmaven.javadoc.skip=true -DskipTests\\"' // Run: mvn release:perform '-Darguments="-Dmaven.javadoc.skip=true' '-DskipTests"'
Ex3: sh 'mvn release:perform -Darguments=\\\"-Dmaven.javadoc.skip=true -DskipTests\\\"' // Run: mvn release:perform '-Darguments="-Dmaven.javadoc.skip=true' '-DskipTests"'
...

How can I do this with jenkins sh or why are single quotes in the resulting command?

@Faheetah
Copy link
Author

Faheetah commented Jan 20, 2021

@carpiupin

Ignore the output of the quoted, Jenkins does something wrong when displaying them on the logs. What you see is probably not what Jenkins is running. The sh step gets pretty unhappy with spaces in commands.

It looks like it should work as is, but maybe try dollar slashy strings? You might need to escape the space. I would also check if the maven plugin can do what you are trying to accomplish, I try to avoid shell commands any chance I get for this reason.

@JuaanO
Copy link

JuaanO commented Apr 14, 2021

How to suppose escape of this:

sh 'mvn test -Dcucumber.options="-t @"${params.Primera}""'

@shadycuz
Copy link

shadycuz commented Apr 14, 2021

@JuaanO I'm not familiar with mvn's arguments but I suspect this will work.

sh "mvn test -Dcucumber.options='-t @${params.Primera}'"

if that doesn't work because you really need a set of quotes after the @ try:

sh """mvn test -Dcucumber.options='-t @"${params.Primera}"'"""

@MarinaGhe
Copy link

MarinaGhe commented Apr 14, 2021

Hello there, I'm trying to set a pipeline with Jenkins and given the fact that I'm new at this I have a big problem with interpolating a custom param variable into a shell command. I also have defined in my Jenkins configuration different deploy environments and in the deployment step if I do something like:

sh """ ${DEPLOY_TO_DEV} """
it works as expected and if I try to do something like:

environment { DEPLOY = "DEPLOY_TO_${params.ENV}" }
steps { sh "echo $DEPLOY" sh "${DEPLOY}" }

I get an error saying that the script was not found or is not executing anything, although it prints DEPLOY_TO_DEV. I've tried some suggestions found on google but without success, I can't figure out what I'm missing.
Any help is appreciated! Thanks

@shadycuz
Copy link

shadycuz commented Apr 14, 2021

@MarinaGhe

A better place to get help is DevOps StackExhage or the Jenkins Gitter.

I'm not sure why your first example would work or not work because I would need to see the rest of your job.

I can however explain the second example.

// if the parameter ENV == 'STAGE'
environment {
    DEPLOY = "DEPLOY_TO_${params.ENV}"
}
// Then this would create an environment variable DEPLOY
// whose value is `DEPLOY_TO_STAGE
// this get passed directly to the shell as is and it works because
// you previously created an environment variable called DEPLOY
sh "echo $DEPLOY"
// This does not get passed directly to the shell.
// Groovy interpolates DEPLOY but I didn't see you create a variable called DEPLOY
// So this should error for variable not found OR maybe it get turned into `null` and passed like that to sh
// this explains your error about command not found
sh "${DEPLOY}"

It seems like you are using declarative pipelines and I don't know much about that, but in scripted pipeline I would use:

String command = "DEPLOY_TO_${params.ENV}"

sh(command)

I wouldn't mess with creating an environment variable unless the script it's self needed it.

@MarinaGhe
Copy link

MarinaGhe commented Apr 14, 2021

@shadycuz

Thanks a lot for your response. The first command works because in my Jenkins configuration I have defined multiple global variables each associated with a deploy command and that's how I access it in my Jenkinsfile. Given the fact that I have multiple deploy environments I wanted to use the custom params value to execute a different deploy command depending on what param is selected. I was suspecting the behaviour you described but I wasn't sure. I haven't found the solution yet but I'll keep try.

@shadycuz
Copy link

shadycuz commented Apr 14, 2021

@MarinaGhe

So you have global variables that are like DEPLOY_TO_DEV and DEPLOY_TO_STAGE?

You want users to enter a parameter called ENV when they run the job and then using that parameters value, you want the contents of the matching global variable?

as an example

String DEPLOY_TO_DEV = 'some_command --some ARGS` // DEPLOY_TO_DEV has the "dev" deploy command

String ENV = 'DEV' // User entered "DEV" for the ENV parameter

if (ENV == 'DEV") {
    sh(DEPLOY_TO_DEV)
}

If this example above is the correct logic then you can do something like this:

steps {
    script {
        if (params.ENV == 'DEV' ) {
            sh "${DEPLOY_TO_DEV}"
        }
        if (params.ENV == 'STAGE' ) {
            sh "${DEPLOY_TO_STAGE}"
        }
    }
}

I think as you get more advanced with jenkins you might want to stop using declarative and move to scripted. This excample in scripted:

Map commands = [
    'DEV' : 'command --arg',
    'STAGE': 'command --arg'
]

sh(commands[params.ENV])

Where commands is a dictionary, like in python and the params.ENV is used as the key.

@JuaanO
Copy link

JuaanO commented Apr 14, 2021

@shadycuz Thanks a lot for you response. Yes, works with:
sh """mvn test -Dcucumber.options='-t @"${params.Primera}"'"""

However now response come with doble quote, how to scape to @"Primera" ?

Running runner.RunCukesTest None of the features at [src/test/java/features] matched the filters: [@"Primera"]

@JuaanO
Copy link

JuaanO commented Apr 14, 2021

@shadycuz Thanks a lot for you response, Yeah works removing two quotes.

sh """mvn test -Dcucumber.options='-t @${params.Primera}'"""

Thanks @shadycuz

@MarinaGhe
Copy link

MarinaGhe commented Apr 14, 2021

@shadycuz

That's exactly what I'm doing already :D (I should have provided the whole script, sorry) I was trying to find out if there is an alternative to the multiple if's and trying to dynamically create the deploy command. But I will look into the scripted pipeline also, seems to be more powerful. Thanks a lot for your time, you rock! Have a great day!

@celesteking
Copy link

celesteking commented May 7, 2021

Jenkins is a piece of garbage. A simple rename xx yy *.log turns to hell.

@sourabhgupta385
Copy link

sourabhgupta385 commented Aug 8, 2021

I want to execute below command in Jenkins
trivy image --format template --template "@contrib/html.tpl" -o trivy-report.html --input helloworld_${BUILD_NUMBER}.tar

In above command note double quotes with @

In Jenkinsfile, I have given instruction as :

sh script: 'trivy --cache-dir /tmp/trivy image --format template --template \"@contrib/html.tpl\" -o trivy-report.html --input helloworld_${BUILD_NUMBER}.tar'

but in jenkins logs I see:

[Pipeline] sh

  • trivy --cache-dir /tmp/trivy image --format template --template @contrib/html.tpl -o trivy-report.html --input helloworld__44.tar

i.e. double quotes is omitted. For the command double quotes and @ needs to be adjacent for it to run properly. Can anyone please help me with this? I've tried a lot of options but nothing works.

@haridsv
Copy link

haridsv commented Aug 8, 2021

@sourabhgupta385 You don't need backslashes for the double-quotes to be seen by shell, i.e., the below would make shell see the double-quotes just fine:

sh script: 'trivy --cache-dir /tmp/trivy image --format template --template "@contrib/html.tpl" -o trivy-report.html --input helloworld_${BUILD_NUMBER}.tar'

What you are seeing in the console is the exact command that is being executed, not the command that is seen by shell. You can test this out by using a standalone script, something like this:

$ cat /tmp/t.sh
set -x
echo "@contrib/html.tpl"
$ bash /tmp/t.sh
+ echo @contrib/html.tpl
@contrib/html.tpl

As you can see above, though the script has double-quotes, what shell shows on the console doesn't have it. The double-quotes are only useful to prevent some processing from occurring and they get stripped before the actual command gets executed.

I recommend explaining the problem that you are trying to solve so that someone could provide a better solution that what you are doing.

@sourabhgupta385
Copy link

sourabhgupta385 commented Aug 8, 2021

@sourabhgupta385 You don't need backslashes for the double-quotes to be seen by shell, i.e., the below would make shell see the double-quotes just fine:

sh script: 'trivy --cache-dir /tmp/trivy image --format template --template "@contrib/html.tpl" -o trivy-report.html --input helloworld_${BUILD_NUMBER}.tar'

What you are seeing in the console is the exact command that is being executed, not the command that is seen by shell. You can test this out by using a standalone script, something like this:

$ cat /tmp/t.sh
set -x
echo "@contrib/html.tpl"
$ bash /tmp/t.sh
+ echo @contrib/html.tpl
@contrib/html.tpl

As you can see above, though the script has double-quotes, what shell shows on the console doesn't have it. The double-quotes are only useful to prevent some processing from occurring and they get stripped before the actual command gets executed.

I recommend explaining the problem that you are trying to solve so that someone could provide a better solution that what you are doing.

Hi @haridsv
I executed :
image

But in Jenkins the command executed is:

image

Since the argument was not in double quotes, the command failed. When I executed it in local with double quotes, everything worked fine.
Therefore, I either need double or single quotes around @contrib/html.tpl

@Faheetah
Copy link
Author

Faheetah commented Aug 8, 2021

The reason it looks like the quotes aren't there is because the Jenkins console will further mangle the command and make it look like the wrong command ran. I've had cases of using quotes and spaces where something like foo bar "baz faz" in the console looked like foo "bar "baz" faz" or something (I forget exactly but it looked awful). What you see in the console can't be trusted. And sadly in that specific case, none of us could figure out how to get Jenkins to play nice with the quotes around the args. You might try using two backslashes before the quote, Jenkins might be swallowing them up.

@haridsv
Copy link

haridsv commented Aug 8, 2021

@sourabhgupta385 The @ symbol is not special in most shells, so there are essentially no special chars in the entire string @contrib/html.tpl and so the command should in theory work without any sort of quotes around it (though having double quotes won't harm it). If it is not working for you, it is most likely due to the workspace missing that file, or not in the correct working directory at the time the command is run. Does the filename actually start with the @ symbol or is it a special character for trivy?

@sourabhgupta385
Copy link

sourabhgupta385 commented Aug 8, 2021

The directory contrib does not start with @. It's a special syntax for trivy to get the report generated in html from the template stored in contrib directory.

My workspace and everything is fine.. somewhere I think @ is treated as special character in Jenkins due to which combination of quote and @ is ignoring quotes. If I put space in between of double quote and @, then those quotes are not ignored while seeing in Jenkins console logs but that way I do not get desired output also. Report is not generated in template format. Rather the report contains the string contrib/html.tpl

@scottmelhop
Copy link

scottmelhop commented Sep 9, 2021

After hours of searching, this saved me. Thankyou!

@pyeprog
Copy link

pyeprog commented May 12, 2022

thank you for the harsh truth!

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