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

Re: problem redeclaring path variable (ksh incompatibility)



On Mar 18,  1:31am, Michael Wardle wrote:
} 
} I have a script that uses the identifier "path" local to a function.  It 
} works as intended in bash and all the versions of ksh I've tried it on, 
} but not in zsh, which gives an error message similar to this:
} addpath:typeset:6: path: can't assign initial value for array

Zsh does not fully emulate the POSIX shell unless it is invoked under the
name "sh".  The "emulate" command is not sufficient; it only changes the
setopts, not the set of special variables etc. that are predeclared at
startup time.  Hence the "path" variable exists and is special and may
only be overridden locally to a function by using "typeset -h path=...".

Try running your test script with ARGV0=sh in the environment and note
the difference in behavior.
 
} It turns out that neither "typeset path=" nor "typeset path=value" 
} create a local scalar, which doesn't meet my expectations.  Strangely, 
} however, "typeset path" does.

No, it doesn't.  Nowhere in your test script is $path a scalar.

zsh% func() { emulate ksh; typeset path ; path=scalar ; typeset path }
zsh% func
path=(scalar)
zsh% 

If we were going to attempt to change this, the right way would be to add
a new option, perhaps called LOCAL_SPECIALS, which would be on by default.
"emulate sh" et al. would unset this option.  When NO_LOCAL_SPECIALS, the
typeset builtin would behave as if the -h option were always present.

Other, less desirable approaches might be to tie this behavior to the
POSIX_BUILTINS or KSH_TYPESET options.  Possibly it should be tied to
KSH_TYPESET even if LOCAL_SPECIALS is added.



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