title: How to echo a shell script to a file in groovy interface like Jenkinsfile
body: <p>I have the below shell script that i need to echo to a file lets say script.sh from a groovy interface like Jenkinsfile but keep getting compilation errors.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>#!/bin/bash
  commit_hash=$(git rev-parse HEAD)
  parent_hashes=`git rev-list --parents -n 1 $commit_hash`
  parent_count=`wc -w &lt;&lt;&lt; $parent_hashes`
  if [[ $parent_count -gt 2 ]]
  then
    p=`git name-rev $parent_hashes | xargs -0 | grep -e '^\S\+ master$'`
    if [[ ! -z $p ]]
    then
      echo "merged branch is master"
      exit 0
    else
      echo "merged branch is anything but master"
      exit 2
    fi
  else
    echo "no branch merged"
    exit 1
  fi</code></pre>
  </div>
  </div>
  </p>
  <p>I tried the below :-</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>sh '''echo '#!/bin/bash
                 commit_hash=$(git rev-parse HEAD)
                 parent_hashes=`git rev-list --parents -n 1 $commit_hash`
                 parent_count=`wc -w &lt;&lt;&lt; $parent_hashes`
                 if [[ $parent_count -gt 2 ]]
                   then
                 p=`git name-rev $parent_hashes | xargs -0 | grep -e '^\S\+ master$'`
                 if [[ ! -z $p ]]
                    then
                 echo "merged branch is master"
                 exit 0
                 else
                echo "merged branch is anything but master"
                 exit 2
                  fi
                 else
                echo "no branch merged"
                 exit 1
                  fi' &gt; script.sh'''</code></pre>
  </div>
  </div>
  
  I see the shell script has single quotes in a line plus a few back slashes, so not sure why groovy is not allowing normal shell interpolation here.
  How do i get to echo the contents of this shell script to a file using groovy. I am trying this out in scripted Jenkinsfile.</p>