Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

simple question regarding local variables



I should know this:

Say several functions will use the same set of variables, so you want
some way of auto creating them for each function, *but* they must be
local to each function.

This works:

'test1' is a script:

      #!/usr/bin/zsh
      local variable=howdy
      local anothervariable=whatever

   local foo=FOO
   local bar=BAR
   local baz=BAZ
   # and so on ... I create all my variables in the script.


'test2' is a function that sources 'test1':

   test2 ()
   {
      varis variable
      .  ./test1
      varis variable
   }


('varis' simply prints the name and the value of variables.)

A run:

   $ test2; varis variable
   $variable is: ""
   $variable is: "howdy"
   $variable is: ""


... just as I want, sourcing 'test1' creates local variables in my function, but why does it work? sourcing I understand to create a child shell, thus how is it that after 'test1' returns, that it's local variables are known to 'test2'? Exactly as I want, '$variable' evaporates after 'test2' returns, so it's local. I like it, but I can't explain it.





Messages sorted by: Reverse Date, Date, Thread, Author