title: Jenkins save shell output to var
body: <p>I'm trying to check the md5sum of a file and export the Okay to a var. Then check that var to mark its okay if its not then fail the build.</p>
  
  <p>How do I go about saving the result of the md5checksum to a variable that I can check in Jenkins.</p>
  
  <p>I found this earlier but it doesn't see to work. I get an error from jerkins anytime I try to run the script.</p>
  
  <pre><code>md5Check = sh( script: 'md5sum -c ${env.SSH_HOME}/MD5SUM.MD5', returnStdout: true ).trim()
  sh "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; echo ${md5Check}\""
  </code></pre>
  
  <p>It doesn't like the first line at all. Is there another way to do this?</p>
  
  <p>Error: </p>
  
  <pre><code>WorkflowScript: 44: Expected a step @ line 44, column 17.
                 md5Check = sh "sudo ssh -i ${env.SSH_KEY} 
  ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; md5sum -c 
  ${env.SSH_HOME}/MD5SUM.MD5;\""
                 ^
  
  1 error
  
  at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I was able to fix this with the 2 supplied answers below but now the sh command runs on the Jenkins side instead of through ssh.</p>
  
  <p>I also found I needed to wrap the code in a script and node for it to run the script.</p>
  
  <pre><code>script{
      node(){
          unstash 'build'
          env.FAIL=sh([script: "md5sum -c ${env.SSH_HOME}/MD5SUM.MD5", returnStdout: true ]).trim()
          sh "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; echo ${env.FAIL}\""
      }
  }
  </code></pre>
  
  <p>So with the echo ${env.FAIL} it calls the correct command now but cant find the file because I think its running it in Jenkins shell not the ssh.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Okay so if I move the ssh stuff into the defined script and then just call echo from Jenkins shell it will find the file on the remote server properly. Here is the final code I used.</p>
  
  <pre><code>script{
      node(){
          unstash 'build'
          env.FAIL=sh([script: "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; md5sum -c ${env.SSH_HOME}/MD5SUM.MD5\"", returnStdout: true ]).trim()
          sh "sudo echo ${env.FAIL}"
       }
  }
  </code></pre>