On 10/14/2013 07:51 AM, Akim Demaille wrote: > Hi! > > I have this weird behavior that I do not understand: when running > > FOO=1 foo > > I do not expect FOO to remain in the env after foo was invoked, even > if foo is a shell function. However it does, _if_ bash is invoked > in POSIX mode, something which I couldn't find documented in the > model (and I wouldn't know where to find this information in POSIX). > > Is this really on purpose? It breaks natural assumptions in /bin/sh > scripting programming :/
Yes, it's really on purpose. POSIX really does have that unfortunate requirement that assignments on the same line as a function call persist beyond the function call (unless the function itself further modifies the environment). http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05 "When a function is executed, it shall have the syntax-error and variable-assignment properties described for special built-in utilities in the enumerated list at the beginning of Special Built-In Utilities." http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14 "Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be the case with a regular built-in or other utility." There is a movement to try and make the next version of POSIX specify 'local'; among other comments in that thread, David Korn (of ksh fame) proposes that POSIX consider standardizing two types of functions: dynamic scoped functions with current POSIX syntax, where assignments persist after the function call (as in bash), and static scope functions introduced with the 'function' keyword and where assignments prior to such function names are automatically treated as 'local' variables and reset when the function completes (as in ksh): http://thread.gmane.org/gmane.comp.standards.posix.austin.general/8371/focus=8377 But bash would have to implement static scoping (in addition to its current dynamic scoping) for that to work. So in the meantime, yes, bash really does behave non-intuitively when in posix mode. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature