Paolo Bonzini <bonzini <at> gnu.org> writes:
> > In the meanwhile I thought about
> >
> > as_lineno=${as_lineno-$LINENO} func
> >
> > which is a slick trick, as it supports nesting and does not require
> > unset.
>
> ... and fails in bash 2.x, of course.
Maybe I couldn't reproduce the failure in bash 2.x, but I do see it in Solaris
8 /bin/sh:
$ func_var(){ echo $a;}
$ func_var
$ a=b func_var
$
Even worse, bash 3.2 is bipolar:
$ bash -c 'func(){ echo "${a+set}";}; a=1 func; echo $a'
set
$ bash -c 'set -o posix;func(){ echo "${a+set}";}; a=1 func; echo $a'
set
1
$
In other words, asking bash for POSIX compliance actually makes bash violate
POSIX by leaking the assignment beyond the function call; since POSIX requires
that "Otherwise, the variable assignments shall be exported for the execution
environment of the command and shall not affect the current execution
environment (except for special built-ins)."
pdksh has the same bug as "Posix-mode" bash.
In summary, asking a shell to temporarily assign environment variables for the
duration of a function call is not portable. So I'm installing this:
From: Eric Blake <[EMAIL PROTECTED]>
Date: Tue, 14 Oct 2008 11:26:58 -0600
Subject: [PATCH] Document shell function environment pitfall.
* doc/autoconf.texi (Shell Functions): Document bugs in bash,
Solaris /bin/sh.
Signed-off-by: Eric Blake <[EMAIL PROTECTED]>
---
ChangeLog | 6 ++++++
doc/autoconf.texi | 17 +++++++++++++++++
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d6337f6..071f2a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-10-14 Eric Blake <[EMAIL PROTECTED]>
+
+ Document shell function environment pitfall.
+ * doc/autoconf.texi (Shell Functions): Document bugs in bash,
+ Solaris /bin/sh.
+
2008-10-14 Paolo Bonzini <[EMAIL PROTECTED]>
Use m4_require to implement AS_REQUIRE.
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index ff81c2e..dc86462 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -14225,6 +14225,23 @@ Shell Functions
and other options upon function entry and exit. Inside a function,
@acronym{IRIX} sh sets @samp{$0} to the function name.
+It is not portable to pass temporary environment variables to shell
+functions. Solaris @command{/bin/sh} does not see the variable. Meanwhile,
[EMAIL PROTECTED] 3.2 breaks the Posix rule that the assignment must not affect
+the current environment, but only when Posix compliance is requested!
+
[EMAIL PROTECTED]
+$ @kbd{/bin/sh -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
[EMAIL PROTECTED]
[EMAIL PROTECTED]
+$ @kbd{bash -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
[EMAIL PROTECTED]
[EMAIL PROTECTED]
+$ @kbd{bash -c 'set -o posix; func()@{ echo $a;@}; a=1 func; echo $a'}
[EMAIL PROTECTED]
[EMAIL PROTECTED]
[EMAIL PROTECTED] example
+
Some ancient Bourne shell variants with function support did not reset
@[EMAIL PROTECTED], @var{i} >= 0}, upon function exit, so effectively the
arguments of the script were lost after the first function invocation.
--
1.6.0.2