Dennis
Dennis

Reputation: 2964

Jenkinsfile: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression

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.

Upvotes: 49

Views: 58683

Answers (2)

MatrixManAtYrService
MatrixManAtYrService

Reputation: 9131

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.

Upvotes: 60

Szymon Stepniak
Szymon Stepniak

Reputation: 42184

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.

Upvotes: 81

Related Questions