Re: making a variable local counts as a statement as per $?
From:
Oğuz
Subject:
Re: making a variable local counts as a statement as per $?
Date:
Tue, 24 Feb 2026 14:04:40 +0300
On Tuesday, February 24, 2026, Luca Saiu <positron@gnu.org> 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