[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: if source command.sh & set -e issue
From: |
Greg Wooledge |
Subject: |
Re: if source command.sh & set -e issue |
Date: |
Wed, 24 Jul 2024 15:08:45 -0400 |
On Wed, Jul 24, 2024 at 20:53:33 +0300, Mor Shalev via Bug reports for the GNU
Bourne Again SHell wrote:
> *if source command.sh ; then echo passfi*
> Or, similarly:
>
> *source command.sh && echo pass*
Remember how -e is defined:
-e 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.
The shell does not exit if the command that fails is
part of the command list immediately following a while
or until keyword, part of the test following the if or
elif reserved words, part of any command executed in a
&& or || list except the command following the final &&
or ||, any command in a pipeline but the last, or if the
command's return value is being inverted with !.
With that in mind, let's re-establisg our setup, with two variants for
the initial script:
hobbit:~$ cat script1.sh
#!/bin/bash
echo script.sh begin
source command.sh && echo pass
echo script.sh end
hobbit:~$ cat script2.sh
#!/bin/bash
echo script.sh begin
source command.sh
test $? = 0 && echo pass
echo script.sh end
hobbit:~$ cat command.sh
set -e
echo command.sh start
false
echo command.sh end
Now compare:
hobbit:~$ bash-5.2 script1.sh
script.sh begin
command.sh start
command.sh end
pass
script.sh end
hobbit:~$ bash-5.2 script2.sh
script.sh begin
command.sh start
So, we can see what happened here. In script1.sh, command.sh is sourced
by a command which is "part of any command executed in a && or || list
except the command following the final && or ||". And therefore, set -e
does not trigger.
In script2.sh, the source command is NOT part of the command list
following while/until, nor part of the test following if/elif, nor any
part of a &&/|| list. And set -e triggers.
So this would appear to be part of an intended change, to make the
behavior of -e satisfy the documented requirements.
Please remember, -e is *not* intended to be useful, nor is it intended
to be intuitive. It's intended to be *bug compatible* with whatever
interpretation the POSIX committee has agreed upon this year. This
interpretation changes over time, so the behavior of -e also changes.