[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: |
Sat, 13 Dec 2003 20:40:18 -0500 |
On Sat, 13 Dec 2003 09:31:07 +0100, Paolo Bonzini wrote:
> Eric Sunshine wrote:
> > 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.
> What if you do not "shift" arguments? Are $*, $@, $<n> then properly
> restored.
No, they are not. The $*, $@, $<n> variables are clobbered by any and all
function calls since they are not localized to the function. For example:
foo() {
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
Outer: bar baz 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 intact:
% bash ./shell.sh wiffles eat smoo
Outer: wiffles eat smoo
Inner: bar baz cow
Outer: wiffles eat smoo
-- ES