Re: making a variable local counts as a statement as per $?

Oğuz <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Message-ID <CAH7i3LpGaaxmJbtL040aRpT3YWnR=ZQ36iY0WWX_CMHzQiZJxA@mail.gmail.com>
On Tuesday, February 24, 2026, Luca Saiu <[email protected]> wrote:

> Hello.
>
> I was surprised to discover that the following two lines behave
> differently:
>
>
>   f () { a=$(false); echo $?; }; f         # This prints 1 as expected
>
>   f () { local a=$(false); echo $?; }; f   # This prints 0
>
> The way I interpret this is that making the variable local “counts as a
> statement”, and overwrites the value of $? with 0, since making the
> variable local succeeds.  Is this correct?  Is this the intended
> semantics?  I certainly find it counterintuitive, and cannot see it
> being documented.


local is a command like everything else, `echo a=$(false)' returns 0 as
well. You can always do

local a
a=$(false)

instead.


>
> Thanks, and thanks for your precious work on Bash.
>
> --
> Luca Saiu      https://ageinghacker.net
>   GNU Jitter     https://www.gnu.org/software/jitter
>   GNU epsilon    https://www.gnu.org/software/epsilon
>   pEp-mail-tool  https://codeberg.org/pEp/pEp-mail-tool
>
> I support everyone's freedom of mocking any opinion or belief, no
> matter how deeply held, with open disrespect and the same unrelented
> enthusiasm of a toddler who has just learned the word "poo".
>


-- 
Oğuz
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.