| Deutsch English Français Italiano |
|
<vleqhh$16m4u$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.misty.com!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Jerry Peters <jerry@example.invalid>
Newsgroups: comp.unix.shell
Subject: Re: An interesting little quirk - difference between "bash" and "dash" (Linux)
Date: Sun, 5 Jan 2025 20:37:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <vleqhh$16m4u$1@dont-email.me>
References: <vldshb$2jmh6$1@news.xmission.com>
Injection-Date: Sun, 05 Jan 2025 21:37:06 +0100 (CET)
Injection-Info: dont-email.me; posting-host="43f233de20a7eb06ba01fe216ebe57ee";
logging-data="1267870"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hgT5fw5eDfd3f8I+FwCGtBPeHSJCZGY4="
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/6.12.7 (x86_64))
Cancel-Lock: sha1:/NevfvVqcQWEAErSzhrUnXxVzl0=
Bytes: 2361
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.