[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 01-as-require-shell-fn.patch
From: |
Eric Sunshine |
Subject: |
Re: 01-as-require-shell-fn.patch |
Date: |
Tue, 9 Dec 2003 08:30:48 -0500 |
On 23 Nov 2003 23:26:51 -0800, Paul Eggert wrote:
> "Paolo Bonzini" <address@hidden> writes:
> > is there a document about the varying degrees of support
> > for functions in different Bourne shells?
> Not that I know of.
I have not seen such a document either, however I can report of at least one
incompatibility which I have encountered when running shell scripts written
by other people. Specifically, more recent Bourne shells localize the
variables $*, $@, $1, $2, ..., during a function call, whereas older shells
do not. This means that it is safe to "shift" arguments within a function in
more recent shells without affecting outer bindings of these variables, but
it is not safe to do so with older shells. Here is an example:
foo() {
echo "--> Inner: $*"
shift
shift
echo "<-- Inner: $*"
}
echo "Outer: $*"
foo "bar" "baz" "cow"
echo "Outer: $*"
When this script is invoked with an older shell, such as the one shipped
with NextStep, the call to foo() destructively changes the outer bindings of
$*, etc., as shown below:
% sh ./shell.sh wiffles eat smoo
Outer: wiffles eat smoo
--> Inner: bar baz cow
<-- Inner: cow
Outer: cow
On the other hand, when invoked with a more recent shell, such as a modern
version of Bash, the outer bindings of $*, etc., remain unchanged:
% bash ./shell.sh wiffles eat smoo
Outer: wiffles eat smoo
--> Inner: bar baz cow
<-- Inner: cow
Outer: wiffles eat smoo
This is an important distinction, and I suspect that most modern programmers
may not realize that these variables will not be localized in older shells,
since they are used to languages in which function arguments typically are
localized. This problem is especially troublesome when people start writing
recursive shell functions.
The Autoconf documentation presently warns against using shell functions at
all, however, perhaps it would be a good idea to document this issue in the
"Writing Portable Shell Scripts" section just for the case when someone feels
that they simply can not get by without using shell functions. I can
document this issue for the Autoconf manual, if people consider it
worthwhile.
-- ES
- Re: 01-as-require-shell-fn.patch,
Eric Sunshine <=