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

<p>See how it behaves if I use echo it these two different cases:</p>

<p>(I have tested this in groovy shell)</p>

<p><strong>1st Case:</strong></p>

<pre><code>jenkinsParam = "test\ntest"
print jenkinsParam
test
test
</code></pre>

<p>** 2nd Case:**</p>

<pre><code>jenkinsParam = "test\\ntest"
print jenkinsParam
test\ntest
</code></pre>

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

<pre><code>output = jenkinsParam.replaceAll('\\\\n', 'yes')
print output
testyestest
</code></pre>

CC BY-SA 4.0
2019-11-19T19:53:30.000Z
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
CC BY-SA 4.0
2019-11-19T19:59:45.367Z
updated the answer. try it out and see if it works for you.
Samit
CC BY-SA 4.0
2019-11-19T20:06:35.590Z
yep you're right - thanks!
Alex
CC BY-SA 4.0
2019-11-19T19:50:09.887Z
2019-11-19T19:59:30.690Z
2019-11-19T19:59:30.690Z
Samit
<p>I set up my jenkins job to take in the parameter <code>payload</code> and set its value to <code>test\ntest</code>.</p>

<p>My jenkins script is the following:</p>

<pre><code>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
</code></pre>

<p>The output is (cleaned up to remove jenkins pipeline echos):</p>

<pre><code>test\ntest

param:
test\ntest

----------

hardset:
testYestest
</code></pre>

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

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

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

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

CC BY-SA 4.0
2019-11-19T19:38:53.517Z
Can you print the value of jenkinsParam and show us what it prints exactly?
Samit
CC BY-SA 4.0
2019-11-19T19:41:23.927Z
@Samit added at the bottom
Alex
CC BY-SA 4.0
2019-11-19T19:50:52.777Z
You can try to do `echo hardset` and see if the behaviour is similar to what I have just answered.
Samit
<jenkins><groovy>
jenkins parameter behaves differently from internally defined variable
Alex
# jenkins parameter behaves differently from internally defined variable

(Asked by: username_0 on 2019-11-19)

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):
test\ntest

param:
test\ntest

----------

hardset:
testYestest

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.

**Tags:** jenkins, groovy

> **Comments:**
> **username_1**: Can you print the value of jenkinsParam and show us what it prints exactly?  
> **username_0**: @username_1 added at the bottom  
> **username_1**: You can try to do `echo hardset` and see if the behaviour is similar to what I have just answered.  

---

## 1 answers

### Answer by username_1 on 2019-11-19. Score: 2 | Accepted answer

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
test
test

** 2nd Case:**
jenkinsParam = "test\\ntest"
print jenkinsParam
test\ntest

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
testyestest

> **Comments:**
> **username_0**: 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  
> **username_1**: updated the answer. try it out and see if it works for you.  
> **username_0**: yep you're right - thanks!  

---

