I set up my jenkins job to take in the parameter payload and set its value to test\ntest.

My jenkins script is the following:

def hardset = "test\ntest"

echo "param:"
def cleanParam = jenkinsParam.replaceAll("[\\n\\t ]","Yes");
echo cleanParam

echo "----------"

echo "hardset:"
def cleanHardset = hardset.replaceAll("[\\n\\t ]","Yes");
echo cleanHardset

The output is (cleaned up to remove jenkins pipeline echos):





Why? What's different between reading in a jenkins parameter and setting one in the script body?

In practice, I'm trying to replace troublesome \n coming in from a lambda payload, but this is throwing a wrench in my plans.

doing something like def hardsetParam = jenkinsParam to set it in the script body doesn't seem to work either, sadly.

In response to a comment - doing echo jenkinsParam gives me test\ntest. To my eyes, it's identical to hardset.

  • Can you print the value of jenkinsParam and show us what it prints exactly?
    – Samit
    Nov 19, 2019 at 19:38
  • @Samit added at the bottom
    – Alex
    Nov 19, 2019 at 19:41
  • You can try to do echo hardset and see if the behaviour is similar to what I have just answered.
    – Samit
    Nov 19, 2019 at 19:50

1 Answer 1


It seems that the jenkinsParam is holding the output after escaping the special characters. You are assuming that jenkinsParam = "test\ntest" and however it looks like it stores it as jenkinsParam = "test\\ntest"

See how it behaves if I use echo it these two different cases:

(I have tested this in groovy shell)

1st Case:

jenkinsParam = "test\ntest"
print jenkinsParam

** 2nd Case:**

jenkinsParam = "test\\ntest"
print jenkinsParam

In order to replace the payload which is stored in the jenkinsParam (2nd case behaviour), you need to replace the complete \\n from the string. Check the example below:

output = jenkinsParam.replaceAll('\\\\n', 'yes')
print output
  • oh interesting - I confirmed that you're right with hardset's behavior as well! In that case do you have any idea how I can actually replace \n with another character? Neither my original substitution nor, say, \\n seems to be working
    – Alex
    Nov 19, 2019 at 19:53
  • updated the answer. try it out and see if it works for you.
    – Samit
    Nov 19, 2019 at 19:59
  • yep you're right - thanks!
    – Alex
    Nov 19, 2019 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.