Search code examples
shellloopsunset

Loop Variables and `unset` in Shell Script


I just wanted to confirm here since I've only tested in dash shell, but do loop variables collide with variables in the outer scope in shell scripts in general? For example

#! /bin/sh

i='1 2 3'
a='a b c'

for i in $a; do
  echo "$i"
done

echo "$i"

This outputs:

a
b
c
c

which makes sense to me. That is, it seems to indicate that I'm right that loop variables will collide (they share the same namespace as the outer scope). I want to know because if I'm using an older-style shell that doesn't have the local command, I want to be sure to unset loop variables I use in functions. The texts I've read cover unset, but don't seem to cover this case.

Am I right?


Solution

  • You to avoid namespace issues .. You can fork your script and put the loop inside that fork ..

    #! /bin/sh
    
    i='1 2 3'
    a='a b c'
    
    function_to_fork(){
      for i in $a; do
         echo "$i"
      done
    }
    
    (function_to_fork)
    
    echo "$i"