Shell function executions delimit scopes for shell parameters.
(Parameters are dynamically scoped.) The typeset
builtin, and its
alternative forms declare
, integer
, local
and readonly
(but not export
), can be used to declare a parameter as being local
to the innermost scope.
When a parameter is read or assigned to, the
innermost existing parameter of that name is used. (That is, the
local parameter hides any less-local parameter.) However, assigning
to a non-existent parameter, or declaring a new parameter with export
,
causes it to be created in the outermost scope.
Local parameters disappear when their scope ends.
unset
can be used to delete a parameter while it is still in scope;
any outer parameter of the same name remains hidden.
Special parameters may also be made local; they retain their special
attributes unless either the existing or the newly-created parameter
has the -h
(hide) attribute. This may have unexpected effects:
there is no default value, so if there is no assignment at the
point the variable is made local, it will be set to an empty value (or zero
in the case of integers).
The following:
typeset PATH=/new/directory:$PATH
is valid for temporarily allowing the shell or programmes called from it to
find the programs in /new/directory
inside a function.
Note that the restriction in older versions of zsh that local parameters were never exported has been removed.