39

My pipeline fails at the sh """ element of the Jenkinsfile. Any idea where things go wrong?

    stage('Install dependencies') {
                when { expression { return params.dependencies } } }
        steps {
            sh """
              apt-get update
                            apt-get install -y openssh-server net-tools inetutils-ping python-pip rubygems
                            apt-get install -y \
                                apt-transport-https \
                                ca-certificates \
                                curl \
                                gnupg2 \
                                software-properties-common

                            curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -

                            add-apt-repository \
                               "deb [arch=amd64] https://download.docker.com/linux/debian \
                               $(lsb_release -cs) \
                               stable"

                            apt-get update
                            apt-get install -y docker-ce docker-ce-cli containerd.io
                            curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
                            chmod +x /usr/local/bin/docker-compose
                            gem install serverspec pygmy
            """
        }
    }

The error message is:

WorkflowScript: 35: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 35, column 17.

2 Answers 2

72

Replace double quotes """ with single quotes '''.

sh '''
    apt-get update 
    //...
'''

Whenever Groovy sees $ inside the double quotes, it treats this string as GString and does string interpolation. However, in your case, a character $ is not used in the context of interpolation and it fails. Alternatively, you could escape \$ but it makes more sense to switch to single quoted string.

3
  • 1
    This is the simplest answer!
    – AKS
    Mar 3, 2020 at 17:19
  • 1
    This is what I need . Thanks
    – Fauzan
    Jun 27, 2021 at 16:27
  • Upvoted because of simplicity and exactly what I was needing. I had a "shell" script which did some perl and other things and used variables for both perl and bash. Escaping everything is ugly, and it's nice to know that Groovy doesn't need that. Oct 15, 2021 at 20:17
41

If you want to use groovy's string interpolation, you can leave the double-quotes, but you have to escape the dollar sign in your subshell expression because groovy doesn't know what to do with a round brace following a dollar sign:

change:

$(lsb_release -cs)

to:

\$(lsb_release -cs)

I have noticed that the error message indicates the wrong line. In my case:

sh """
   echo "message: ${env.MESSAGE}" # <-- error message points here
   pwd
   echo "ls: $(ls)" <-- this line has the problem
"""

Look for the first dollar sign after the line indicated by the error message.

2
  • 6
    +1 for calling out that the Groovy compiler does not always report the correct location of the error in source.
    – Ben Amos
    Jan 22, 2021 at 18:06
  • same, wrong line number reported was the culprit (i.e. a bug in Groovy)
    – Sohail Si
    May 6, 2021 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.