[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] errexit and attempting to change readonly functions
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] errexit and attempting to change readonly functions |
Date: |
Fri, 27 Sep 2019 21:28:24 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-09-27 12:12:10 +0200, Andreas Kusalananda Kähäri:
> A function can be made read-only with
>
> readonly -f func
>
> This stops it from being unset or redefined:
>
> $ func () { echo hello; }
> bash: func: readonly function
>
> However, with the errexit shell option set, I would expect the shell
> to exit when doing this, just like it exits when trying to modify a
> read-only variable under errexit:
>
> $ set -e
> $ func () { echo hello; }
> $ readonly -f func
> $ func () { echo hi; }
> bash: func: readonly function
> $ # I'm still here
>
> $ set -e
> $ readonly var=42
> $ var=32
> bash: var: readonly variable
> (shell terminates)
>
> What's the rationale behind this, or is it a bug? Or did I simply miss
> some text in the manual?
[...]
I'd vote for a bug, though the code looks a bit like it may be
intentional.
$ bash5 -ec '""() { echo x;}; echo x'
bash5: `""': not a valid identifier
x
Also doesn't trigger errexit, except in posix mode (by design
from the code it seems).
"readonly -f" is not POSIX, so there's nothing POSIX can say
about the behaviour when trying to assign a readonly function
It seems as if bash tried to avoid triggering errexit when it
can get away (wrt posix conformance) with not to.
export() { echo x; }
also causes an error (and triggers errexit) in POSIX mode.
Another example is
readonly x
for x in 1 2; do :; done
(POSIX code) That does trigger errexit.
But:
for ((+;;)); do : ; done
(non-POSIX code)
doesn't.
--
Stephane