Deutsch   English   Français   Italiano  
<831pxhuf41.fsf@helmutwaitzmann.news.arcor.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Helmut Waitzmann <nn.throttle@xoxy.net>
Newsgroups: comp.unix.shell
Subject: Re: An interesting little quirk - difference between "bash" and "dash" (Linux)
Date: Sun, 05 Jan 2025 22:26:22 +0100
Organization: A noiseless patient Spider
Lines: 53
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <831pxhuf41.fsf@helmutwaitzmann.news.arcor.de>
References: <vldshb$2jmh6$1@news.xmission.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Date: Sun, 05 Jan 2025 22:27:36 +0100 (CET)
Injection-Info: dont-email.me; posting-host="6186cac13c7b6533e0589d4c8e40e251";
	logging-data="1292159"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19vE52ZgeUDVwKP+HO/frfXETZXHIeB0Rk="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:q9fNOOgV2yLDTPXfGu70HU46K0I=
	sha1:wKmYdd7tJNPw5uWMCDszHK9uZcY=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Bytes: 2824

 gazelle@shell.xmission.com (Kenny McCormack):
> $ bash -c 'foo=3Dbar;myfun() { local foo; echo "In myfun(), foo =3D $foo"=
; };myfun'
> In myfun(), foo =3D
> $ dash -c 'foo=3Dbar;myfun() { local foo; echo "In myfun(), foo =3D $foo"=
; };myfun'
> In myfun(), foo =3D 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 surpris=
ed
> 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=20
 the dash behavior is part of the POSIX standard.=20


> 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=E2=80=90initialized variable.=C2=A0 It will give=
 you=20
 an modified, emptied variable, while dash will give you an=20
 unmodified variable.=20


 By the way, neither behavior will give you a fresh variable.=C2=A0 Try=20


   (
     for shell in bash dash
     do
       printf '\n%s:\n' "$shell"
       "$shell" -c '
         foo=3Dbar && readonly -- foo &&
         myfun()
         {
           local foo
           printf "In myfun(), foo =3D %s\n" "$foo"
           foo=3D
         } &&
         myfun' "$shell"
     done
   )