Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-61309

Environment variable value containing a literal dollar gets an extra dollar prefixed

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • kubernetes-plugin
    • None

      Description:

      When using the kubernetes plugin and executing a script inside of a container block, environment variables that contain a literal dollar in their value appear to have an extra dollar added before the existing literal dollar.

      E.g. if the environment and container block were:

      environment {
        FOO="\$BAH"
      }
      steps {
        container('busybox') {
          sh 'echo "FOO=$FOO"'
        }
      }
      

      then the console output shows:

      [Pipeline] withEnv
      [Pipeline] {
      [Pipeline] container
      [Pipeline] {
      [Pipeline] sh
      + echo 'FOO=$$BAH'
      FOO=$$BAH
      [Pipeline] }
      [Pipeline] // container
      [Pipeline] }
      [Pipeline] // withEnv
      

      Expected behaviour:

      the value of the environment variable is not altered when referenced inside the container block.

      Work around:

      None that I've found yet.

      Other info:

      Some other permutations I've tried:

      • When the script is executed outside of the container block, the environment variable value is unchanged.
      • When the value of the environment variable contains a literal dollar not at the start of the string, the value is still changed.  E.g. A value of "Something \$BAH" echoes as "Something $$BAH"

      Environment where the bug was seen:

      Jenkins: CloudBees Core Managed Master 2.204.3.2-rolling
      Kubernetes plugin: 1.23.1

          [JENKINS-61309] Environment variable value containing a literal dollar gets an extra dollar prefixed

          Paul Woolley added a comment -

          FWIW, an ugly workaround I'm using is to use "@@" as a placeholder for "$" in the value of the env var and then use sed to once inside the container block to fix up the env var value.  E.g.:

          environment {
            FOO="@@BAH"
          }
          steps {
            container('busybox') {
              sh '''
                #!/bin/bash
                set +x
                echo "Before: FOO=$FOO"
                ## Replace "@@" placeholder with "$"
                FOO=$( echo $FOO | sed 's/@@/\$/g' )
                echo "After: FOO=$FOO"
              '''
            }
          }
          

          gives the desired result in the console output:

          Before: FOO=@@BAH
          After: FOO=$BAH
          

          Paul Woolley added a comment - FWIW, an ugly workaround I'm using is to use "@@" as a placeholder for "$" in the value of the env var and then use sed  to once inside the container block to fix up the env var value.  E.g.: environment { FOO= "@@BAH" } steps { container( 'busybox' ) { sh ''' #!/bin/bash set +x echo "Before: FOO=$FOO" ## Replace "@@" placeholder with "$" FOO=$( echo $FOO | sed 's/@@/\$/g' ) echo "After: FOO=$FOO" ''' } } gives the desired result in the console output: Before: FOO=@@BAH After: FOO=$BAH

            Unassigned Unassigned
            pwoolley Paul Woolley
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: