title: How to pass variables from Jenkinsfile to shell command
body: <p>I want to use a variable which I am using inside of my <code>Jenkinsfile</code> script, and then pass its value into a shell script execution (either as an environment variable or command line parameter).</p>
  
  <p>But the following <code>Jenkinsfile</code>:</p>
  
  <pre><code>for (i in [ 'a', 'b', 'c' ]) {
      echo i
      sh 'echo "from shell i=$i"'
  }
  </code></pre>
  
  <p>Gives the output:</p>
  
  <pre><code>a
  from shell i=
  b
  from shell i=
  c
  from shell i=
  </code></pre>
  
  <p>Desired output is something like:</p>
  
  <pre><code>a
  from shell i=a
  b
  from shell i=b
  c
  from shell i=c
  </code></pre>
  
  <p>Any idea how to pass the value of <code>i</code> to the shell scipt?</p>
  
  <p><strong>Edit:</strong> Based upon <a href="https://stackoverflow.com/a/41550146/2880699">Matt's</a> answer, I now use this solution:</p>
  
  <pre><code>for (i in [ 'a', 'b', 'c' ]) {
      echo i
      sh "i=${i}; " + 'echo "from shell i=$i"'
  }
  </code></pre>
  
  <p>The advantage is, that I don't need to escape the <code>"</code> in the shell script.</p>