Sujet : Re: An interesting little quirk - difference between "bash" and "dash" (Linux)
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 05. Jan 2025, 22:26:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <831pxhuf41.fsf@helmutwaitzmann.news.arcor.de>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
gazelle@shell.xmission.com (Kenny McCormack):
$ bash -c 'foo=bar;myfun() { local foo; echo "In myfun(), foo = $foo"; };myfun'
In myfun(), foo > $ dash -c 'foo=bar;myfun() { local foo; echo "In myfun(), foo = $foo"; };myfun'
In myfun(), foo = bar
$
>
The difference is that in dash, the local foo picks up the value of the
global foo, while in bash, it is empty.
>
I'm not standards obsessed like some people in these newsgroups; I care
more about desirable functionality. In this case, I would not be surprised
to hear that the dash behavior is more POSIX-ly correct,
>
I guess, you won't be surprised to read that neither the bash nor the dash behavior is part of the POSIX standard.
but it seems clear to me that the bash behavior is more
desirable.
>
I frequently use "local" precisely to ensure that I get a fresh,
un-initialized variable.
Bash won't give you an un‐initialized variable. It will give you an modified, emptied variable, while dash will give you an unmodified variable. By the way, neither behavior will give you a fresh variable. Try (
for shell in bash dash
do
printf '\n%s:\n' "$shell"
"$shell" -c '
foo=bar && readonly -- foo &&
myfun()
{
local foo
printf "In myfun(), foo = %s\n" "$foo"
foo } &&
myfun' "$shell"
done
)