Sujet : Re: An interesting little quirk - difference between "bash" and "dash" (Linux)
De : jerry (at) *nospam* example.invalid (Jerry Peters)
Groupes : comp.unix.shellDate : 05. Jan 2025, 21:37:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vleqhh$16m4u$1@dont-email.me>
References : 1
User-Agent : tin/2.4.5-20201224 ("Glen Albyn") (Linux/6.12.7 (x86_64))
Kenny McCormack <
gazelle@shell.xmission.com> wrote:
Consider these two command lines (run from a bash shell, but that's
actually not relevant):
$ 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, 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. Yes, I know that you can always do: local foo=''
but that takes the fun out of it.
It's optional in bash, see typeset:
The -I option causes local variables to inherit the
attributes (except the nameref attribute) and value of any existing
variable with the same name at a surrounding scope. If there is no
existing variable, the local variable is ini???
tially unset.
That said, I always try to initialize locals if they're not
unconditionally set in the cide.