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

Re: [BUG] functions can't create global readonly variables

On Jan 2,  4:27am, Martijn Dekker wrote:
} It is apparently impossible to create global readonly variables from
} within a function.

    typeset -gr var=value
    readonly -g var=value

} I don't know if this is intended behaviour for native zsh mode; the
} 'zshbuiltins' man page does not mention anything about this.

Of course it does:

	 Same as typeset -r.

    typeset ...
	 Set or display attributes and values for shell parameters.

	 A parameter is created for each NAME that does not already refer
	 to one.  When inside a function, a new parameter is created for
	 every NAME (even those that already exist), and is unset again
	 when the function completes.

} But it is certainly a bug for POSIX/'emulate sh' mode.

I think it's undefined behavior for POSIX/'emulate sh'.  Certainly bash
agrees with us:

$ foo() { typeset -r bar; typeset -p bar; }
$ foo
declare -r bar
$ typeset -p bar
bash: typeset: bar: not found

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