1

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?

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Roland Bär
  • 1,720
  • 3
  • 22
  • 33
  • Does this answer your question? [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Nov 18 '22 at 08:48
  • @tripleee The answer from @ KamilCuk solved my problem. In the answer provided by you I miss the information about the meaning of '&' in shell. – Roland Bär Nov 18 '22 at 12:05
  • I'll be more than happy to add https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean to the list of duplicates if you accept the duplicate nomination. – tripleee Nov 18 '22 at 12:07
  • @tripleee although the question handles the problem of the meaning of '&' in the shell I don't see this as a duplicate. My problem was based of the behavior of the '&' inside a command parameter. Your second suggestion is focused on the & at the ending and therefore I think I wouldn't have found the solution there. – Roland Bär Nov 21 '22 at 11:08
  • Either you should accept that this is a shell metacharacter and that metacharacters need to be escaped or quoted; or you genuinely care what it means. You seem to be moving the goalposts here. – tripleee Nov 21 '22 at 11:13

1 Answers1

1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111