Home > Software engineering >  How to quote a path for Jenkins' bat-Command?
How to quote a path for Jenkins' bat-Command?

Time:03-10

I have a problem constructing a bat-Command to be executed on Windows (via Jenkin's declarative pipeline). The path in variable path may contain blanks, so it needs to be quoted. After some googling, I came up with this:

 rjc = bat(script: "\\"${path}\\"" ${cmdline}" , returnStatus: true)

but that errors as follows:

WorkflowScript: 89: unexpected token: $ @ line 89, column 50.
           rjc = bat(script: "\\"${path}\\"
                                 ^

CodePudding user response:

When you use double back slash (\\) the first slash actually escapes the seconds one - which means it is taken as a backslash character instead of an escape operator. so the double quotes (") are actually not escaped, the string is then closed and the dollar sign is unexpected as it has no context.

What you actually want is to escape the double quotes so it will be used as a character instead of closing the string. For that you only need a single back slash:

bat(script: "\"${path}\" ${cmdline}" , returnStatus: true)
  • Related