Alex
Alex

Reputation: 1375

Syntax error while using backslash in Jenkinsfile

I try to make simple pipeline on Jenkins to remove files from few directories time to time. I decided not to create python script with Jenkinsfile as new project, instead of it I try to define new pipeline script in Jenkins job.

pipeline {

  agent any

  stages {
    stage('Check virtualenv') {
      steps {
          sh """
            rm -r /mnt/x/some/directory/Problem\ 1.0/path
          """
      }
    }
  }
}

And I got an error WorkflowScript: 4: unexpected char: '\'. How can I use path with whitespace on it without using backslash? Any other ideas how define path?

Upvotes: 29

Views: 55981

Answers (2)

mkobit
mkobit

Reputation: 47269

The '\' character is a special character in Groovy. If you tried to compile this kind of code with the normal Groovy compiler, it would give you a better error message. The easiest way to handle it would be to escape it:

"""
  rm -r /mnt/x/some/directory/Problem\\ 1.0/path
""" 

Upvotes: 57

Here_2_learn
Here_2_learn

Reputation: 5451

You can modify the shell command as follows:

      sh """
        rm -r /mnt/x/some/directory/Problem""" +  """ 1.0/path"""

Provide space before 1.0 as required. Hope this helps.

Upvotes: 3

Related Questions