Skip to content

Instantly share code, notes, and snippets.

@agarthetiger
Last active June 22, 2025 19:16
Show Gist options
  • Select an option

  • Save agarthetiger/c25afa0a13dcc97c3d2d5362590567a5 to your computer and use it in GitHub Desktop.

Select an option

Save agarthetiger/c25afa0a13dcc97c3d2d5362590567a5 to your computer and use it in GitHub Desktop.

Revisions

  1. agarthetiger revised this gist Jun 1, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions QuotesInJenkinsfiles.groovy
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ node{
    sh 'echo Dropping double quotes in build "$BUILD_NUMBER"'

    // To preserve double quotes, they need to be escaped with double
    // forward slashes. Note that the line echo'd because of the default
    // back slashes. Note that the line echo'd because of the default
    // -x sh option is different from the string which is echo'd. See
    // console output for this job.
    sh 'echo Preserving double quotes in build \\"$BUILD_NUMBER\\"'
    @@ -37,7 +37,7 @@ node{
    // otherwise ambiguous within the string.
    echo "$env.BUILD_NUMBER"

    // A single forward slash is required to delimit the dollar if the
    // A single backslash is required to delimit the dollar if the
    // string should be printed exactly as is.
    echo "Exact print of text \$env.BUILD_NUMBER"

    @@ -48,7 +48,7 @@ node{

    sh "echo Single quotes 'inside' double quotes need no dereferencing."

    sh "echo Double quotes need triple forward slashed to preserve, like \\\" "
    sh "echo Double quotes need triple backslashes to preserve, like \\\" "

    sh """
    echo Single quotes 'inside' double quotes from
  2. agarthetiger created this gist May 31, 2018.
    87 changes: 87 additions & 0 deletions QuotesInJenkinsfiles.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    node{
    timestamps{
    stage('Samples'){

    // Single quotes with no interpolation, at least not in Jenkins.
    // The dollar variable will be evaluated before being run by the
    // shell command, so variables which Jenkins makes available as
    // environment variables can still be accessed from within a
    // single-quoted string, passed to the sh task.
    sh 'echo $PATH'

    // Triple single quotes doesn't change how the interpolation works,
    // only allows spanning multiple lines to execute multiple commands.
    sh '''
    echo The build number is $BUILD_NUMBER
    '''

    // Double quotes are silently dropped. Note that $BUILD_NUMBER here
    // is being evaluated by the shell. There is no interpolation by
    // Groovy or Jenkins before this single-quoted string is being passed
    // to the sh task.
    sh 'echo Dropping double quotes in build "$BUILD_NUMBER"'

    // To preserve double quotes, they need to be escaped with double
    // forward slashes. Note that the line echo'd because of the default
    // -x sh option is different from the string which is echo'd. See
    // console output for this job.
    sh 'echo Preserving double quotes in build \\"$BUILD_NUMBER\\"'

    // As in any programming language, variables in code don't need a
    // dollar prefix or braces.
    echo env.BUILD_NUMBER

    // Groovy will interpolate anything beginning with a dollar sign in
    // a double-quoted string. In this case there was no need to include
    // curly braces, these are only required if the variable name is
    // otherwise ambiguous within the string.
    echo "$env.BUILD_NUMBER"

    // A single forward slash is required to delimit the dollar if the
    // string should be printed exactly as is.
    echo "Exact print of text \$env.BUILD_NUMBER"

    // Jenkins will interpolate the variable in this string, so will
    // pass "echo n" to the sh task, where n is the current build number.
    // Note that env is a variable made available in groovy by Jenkins.
    sh "echo Build number from double-quoted string is $env.BUILD_NUMBER"

    sh "echo Single quotes 'inside' double quotes need no dereferencing."

    sh "echo Double quotes need triple forward slashed to preserve, like \\\" "

    sh """
    echo Single quotes 'inside' double quotes from
    echo multi-line triple double quoted sh task.
    """

    // Example of outputting double quotes into a file, as a workaround
    // for not having JSON groovy methods whitelisted across Jenkins
    // Masters. This works with variables which sh can interpolate, ie.
    // environment variables.
    sh '''
    echo "{\\"build_number\\":\\"$BUILD_NUMBER\\"}" > temp.json
    cat temp.json
    '''

    // There are two other options, either change the enclosing quotes
    // to double quotes and adjust the inner quotes and delimiters.
    def local_build_number = env.BUILD_NUMBER
    sh """
    echo "{\\\"build_number\\\":\\\"$local_build_number\\\"}" > temp2.json
    cat temp2.json
    """

    // or wrap the wingle quoted example with withenv.
    // Note that the snippet generator put single quotes around the
    // variable which need to be double quotes here in order to
    // interpolate BUILD_NUMBER.
    withEnv(["myvar=$BUILD_NUMBER"]) {
    sh '''
    echo {\\"build_number\\":\\"$myvar\\"} > temp3.json
    cat temp3.json
    '''
    }
    }
    }
    }