title: Why does Groovy's .equals() fail in a jenkins pipeline script when interpolating parameters into a double quoted string?
body: <p>(Note:  I have looked at similar questions (such as <a href="https://stackoverflow.com/questions/49677494/comparison-with-string-doesnt-work-in-jenkins-pipeline">this</a> where the problem is failure to trim output from shell commands) but I think this case is distinct.)</p>
  <p>I have a pipeline script in Groovy that uses parameters (via <code>properties([parameters([...</code>).  When I interpolate the value of the parameter into a double quoted string, it fails a <code>.equals()</code> check against both captured (by which I mean &quot;captured <em>and .trim()d!</em>&quot;) stdout (which is my use case) and even a simple string literal.</p>
  <p>I can work around the problem using <code>.trim()</code>, (even though you can see, both through my echoing them and checking the <code>.length()</code>, that there is nothing to <code>.trim()</code>), but I suspect that only &quot;works&quot; because it does an implicit <code>.toString()</code>--which is also a successful workaround.</p>
  <p>This looks like a bug to me, but it's literally my first week working with Groovy, so perhaps I'm missing something--can anyone explain what?</p>
  <p>Even a simple literal <code>&quot;foo&quot;</code> fails (i.e. <code>&quot;foo&quot;.equals(&quot;${params.foo_the_parameter}&quot;</code>).  Is the interpolated parameter some other kind of object or something?</p>
  <p>[<strong>EDIT</strong> After getting the answer from @Matias Bjarland, I modified the code below to use println instead of shelled echo because it makes the output more concise.  The solution he suggested is reflected in a commented block.]</p>
  <p>My groovy code:</p>
  <pre><code>node() {
      properties([
          parameters([
              string(
                  defaultValue: 'foo',
                  description: 'This is foo',
                  name: 'foo_the_parameter'
              )
          ])
      ])
  
      /* this is what I learned from the accepted answer
      bob=&quot;${params.foo_the_parameter}&quot;
      println(&quot;class of interpolated param is ${bob.class}&quot;)
      simple_foo=&quot;foo&quot;
      println(&quot;class of \&quot;foo\&quot; is ${simple_foo.class}&quot;)
      */
      echoed_foo = sh(script:&quot;echo 'foo'&quot;, returnStdout: true).trim()
      println &quot;echoed foo is [$echoed_foo], params foo is [${params.foo_the_parameter}]&quot;;
      echo_foo_length = echoed_foo.length()
      dqs_foo_length = &quot;${params.foo_the_parameter}&quot;.length()
      println &quot;their lengths are: echo: [$echo_foo_length] and dqs: [$dqs_foo_length]&quot;;
      if (echoed_foo.equals(&quot;${params.foo_the_parameter}&quot;)) {
          println &quot;SUCCESS they are equals()&quot;
      }
      else {
          println &quot;FAIL they are not equals()&quot; //this one fires
      }
      if (echoed_foo.equals(&quot;${params.foo_the_parameter}&quot;.trim())) {
          println &quot;SUCCESS they are equals() after the dqs gets a trim()&quot; //this one fires
      }
      else {
          println &quot;FAIL they are not equals()after the dqs gets a trim()&quot;
      }
      if (echoed_foo.equals(&quot;${params.foo_the_parameter}&quot;.toString())) {
          println &quot;SUCCESS they are equals() after the dqs gets a toString()&quot; //this one fires
      }
      else {
          println &quot;FAIL they are not equals()after the dqs gets a toString()&quot;
      }
  
      if (&quot;foo&quot;.equals(&quot;${params.foo_the_parameter}&quot;)) {
          println &quot;SUCCESS at least a simple literal \&quot;foo\&quot; works&quot;
      }
      else {
          println &quot;FAIL even a simple literal \&quot;foo\&quot; fails to be .equals() with the interpolated parameter&quot; //this one fires
      }
  }
  </code></pre>
  <p>Jenkins output:</p>
  <pre><code>Started by user Michael South
  [Office365connector] No webhooks to notify
  Obtained jenkins.groovy from git git@github.redacted.com:msouth/test_groovy_equals.git
  Running in Durability level: MAX_SURVIVABILITY
  [Pipeline] node
  Running on subnet_mon_02 in /opt/jenkins/m1/workspace/field-analytics-org/test_string_equals
  [Pipeline] {
  [Pipeline] properties
  [Pipeline] sh
  [test_string_equals] Running shell script
  + echo foo
  [Pipeline] echo
  echoed foo is [foo], params foo is [foo]
  [Pipeline] echo
  their lengths are: echo: [3] and dqs: [3]
  [Pipeline] echo
  FAIL they are not equals()
  [Pipeline] echo
  SUCCESS they are equals() after the dqs gets a trim()
  [Pipeline] echo
  SUCCESS they are equals() after the dqs gets a toString()
  [Pipeline] echo
  FAIL even a simple literal &quot;foo&quot; fails to be .equals() with the interpolated parameter
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  [Office365connector] No webhooks to notify
  Finished: SUCCESS
  </code></pre>