title: Scope of for loop control variable in shell?
body: <p>I have a question about for loop in shell.</p>
  
  <p>Let's assume this simple shell script:</p>
  
  <pre><code>#!/bin/sh
  loop() {                                                                                                                                                                                                                                                                                                               
      for i in 1 2 3 4; do                                                                                                                                           
          if [ $i -eq 2 ]; then                                                                                                                                      
              [ $1 -eq 2 ] &amp;&amp; return 1                                                                                                                           
              loop $(($1 + 1)) &amp;&amp; return 1                                                                                                                     
          fi                                                                                                                                                         
      done                                                                                                                                                           
  return 1                                                                                                                                                       
  }                                                                                                                                                                  
  
  loop 0       
  </code></pre>
  
  <p>All variables are global, except for arguments (and function arguments). So if I want a local variable in function
  I would have to pass it as argument.</p>
  
  <p>I tried to run this simple script, but I'm not sure if also the for loop list (1 2 3 4 in this example) is also local? 
  See below:</p>
  
  <pre><code>+ loop 0
  + for i in 1 2 3 4
  + '[' 1 -eq 2 ']'
  + for i in 1 2 3 4
  + '[' 2 -eq 2 ']'
  + '[' 0 -eq 2 ']'
  + loop 1
  + for i in 1 2 3 4
  + '[' 1 -eq 2 ']'
  + for i in 1 2 3 4
  + '[' 2 -eq 2 ']'
  + '[' 1 -eq 2 ']'
  + loop 2
  + for i in 1 2 3 4
  + '[' 1 -eq 2 ']'
  + for i in 1 2 3 4
  + '[' 2 -eq 2 ']'
  + '[' 2 -eq 2 ']'
  + return 1
  + for i in 1 2 3 4
  + '[' 3 -eq 2 ']'
  + for i in 1 2 3 4
  + '[' 4 -eq 2 ']'   &lt;- here is $i == 4
  + return 1
  + for i in 1 2 3 4
  + '[' 3 -eq 2 ']'   &lt;- here is $i == 3, correctly behaving as local variable ...
  + for i in 1 2 3 4
  + '[' 4 -eq 2 ']'
  + return 1
  </code></pre>
  
  <p>Can anyone please tell me, how the for loop works internally? I am bit confused about the for loop list, that is behaving like "local variable".</p>
  
  <p>Thank you very much for all your answers! :)</p>