[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Yet another shell function prerequisite!
From: |
Eric Blake |
Subject: |
Re: [PATCH] Yet another shell function prerequisite! |
Date: |
Tue, 14 Oct 2008 17:31:21 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
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 <address@hidden>
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 <address@hidden>
---
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 <address@hidden>
+
+ Document shell function environment pitfall.
+ * doc/autoconf.texi (Shell Functions): Document bugs in bash,
+ Solaris /bin/sh.
+
2008-10-14 Paolo Bonzini <address@hidden>
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,
address@hidden 3.2 breaks the Posix rule that the assignment must not affect
+the current environment, but only when Posix compliance is requested!
+
address@hidden
+$ @kbd{/bin/sh -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
address@hidden
address@hidden
+$ @kbd{bash -c 'func()@{ echo $a;@}; a=1 func; echo $a'}
address@hidden
address@hidden
+$ @kbd{bash -c 'set -o posix; func()@{ echo $a;@}; a=1 func; echo $a'}
address@hidden
address@hidden
address@hidden example
+
Some ancient Bourne shell variants with function support did not reset
@address@hidden, @var{i} >= 0}, upon function exit, so effectively the
arguments of the script were lost after the first function invocation.
--
1.6.0.2
- [PATCH] Yet another shell function prerequisite!, Paolo Bonzini, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Eric Blake, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Eric Blake, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Paolo Bonzini, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Paolo Bonzini, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Ralf Wildenhues, 2008/10/13
- Re: [PATCH] Yet another shell function prerequisite!, Paolo Bonzini, 2008/10/14
- Re: [PATCH] Yet another shell function prerequisite!, Paolo Bonzini, 2008/10/14
- Re: [PATCH] Yet another shell function prerequisite!, Eric Blake, 2008/10/14
- Re: [PATCH] Yet another shell function prerequisite!,
Eric Blake <=
- Re: [PATCH] Yet another shell function prerequisite!, Eric Blake, 2008/10/14