Pipeline - Single quotes are lost when passing a String to a sh() step

Article ID:360036543511
1 minute readKnowledge base

Issue

String arg1 = "arg1"
String arg2 = "arg2"

String cm = "curl -H \\'Authorization: ${arg1}\\' -H \\'X-CUSTOM-HEADER:${arg2}\\' www.google.com"

node {
  sh cm
}

The expected output is:

[Pipeline] {
[Pipeline] sh
+ curl -H 'Authorization: arg1' -H 'X-CUSTOM-HEADER:arg2' www.google.com

Actual output is:

Output:
[Pipeline] sh
+ curl -H ''\''Authorization:' 'arg1'\''' -H ''\''X-CUSTOM-HEADER:arg2'\''' www.google.com

Resolution

No action is required.

The "weird" escaping is just for the build console output of the command that was run, which escapes things one more level than what was actually received (to prevent things like ANSI escape codes from applying to that output).

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.