help-bash
[Top][All Lists]
Advanced

[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: Sat, 28 Sep 2019 07:35:56 +0100
User-agent: NeoMutt/20171215

2019-09-27 16:43:59 -0400, Greg Wooledge:
> On Fri, Sep 27, 2019 at 09:28:24PM +0100, Stephane Chazelas wrote:
> > It seems as if bash tried to avoid triggering errexit when it
> > can get away (wrt posix conformance) with not to.
> 
> The man page says,
> 
>     Exit  immediately  if a pipeline (which may consist of a
>     single simple command), a list, or  a  compound  command
>     (see SHELL GRAMMAR above), exits with a non-zero status.
> 
> I don't see function declarations included under "compound command"
> in that section.  So the question, for me, is whether a function
> declaration counts as a pipeline, a list, or a compound command (or
> none of the above).
> 
> I'm about 90% sure it doesn't count as a compound command, since
> the grammar for a function declaration includes "compound-command"
> right after the "()" part.  Not sure about pipeline/list.
[...]

Note that a compound command is also a pipeline and a pipeline
is also a list. So that text can be simplified to:

     Exit  immediately  if a list (see SHELL GRAMMAR above),
     exits with a non-zero status.

A function definition is also a pipeline and a list, so should
trigger errexit when failing as per that doc.

So even if the current behaviour for

x() { x; }
readonly -f x
x() { y; }

does not break POSIX compliance (the behaviour is unspecified
per POSIX as it doesn't specify the -f option of readonly. Or
IOW such a script is a bash script, not a POSIX sh script, so
bash defines how it should work, not POSIX), it does break "bash
compliance" (goes against its documentation, at least wrt to the
part you quote).

To me, it doesn't make sense not to exit here. If you can't
define a function, that renders the rest of the script (which
presumably uses that function) invalid, so it's better to exit
at least with set -e.

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]