Home > Enterprise >  How to excape '&' in a sh()-call in jenkins
How to excape '&' in a sh()-call in jenkins

Time:11-18

I want to call curl on a webpage with get parameters from the jenkins-groovy-script via sh:

sh("curl http://example.com/page.cgi?param1=a&param2=b&param3=c")

But the command is split at the '&' and in the output log of jenkins there is something like:

  param3=c
  curl http://example.com/page.cgi?param1=a
  param2=b

Of course the call to the web server only contains the first parameter.

I tried already with different kind of escaping but without success.

How can I make the call including all the parameters?

CodePudding user response:

Just like in the command line, you have to quote it or escape it. Research quoting and escaping in sh shell.

Try:

sh("curl 'http://example.com/page.cgi?param1=a&param2=b&param3=c'")
sh('curl "http://example.com/page.cgi?param1=a&param2=b&param3=c"')
sh("curl http://example.com/page.cgi?param1=a\\&param2=b&param3=c")
sh("curl \"http://example.com/page.cgi?param1=a&param2=b&param3=c\"")

& in shell means to run something in the background, like sleep 10 & wait.

  • Related