On 1/4/25 2:47 PM, Tobi Laskowski wrote:
Hello,

I'd like to ask about an odd behaviour relating to the SHELLOPTS environment 
variable. This is present in version 5.2.37(1)-release and in the development 
version.

It's the behavior of every bash version going back to bash-2.0.


In this case, the `nounset` option is only applied to the current shell and not 
to nested bash invocations:
set -o nounset; echo $SHELLOPTS; bash -c "echo \$SHELLOPTS"

Because SHELLOPTS is not exported by default.


On the other hand, if there is a pre-existing SHELLOPTS environment variable 
(set outside the current shell), then the `nounset` option applies to all 
invocations of bash within the current shell. Even with the `+u` flag, it is 
not possible to avoid the `nounset` option being applied to the inner bash 
invocation.

Variables present in the shell's environment are automatically exported.
You can avoid `nounset' being in SHELLOPTS by unsetting it (temporarily,
if you like) in the current shell. You can avoid it being exported by
removing the export attribute (export -n SHELLOPTS).


The specific case where I found this behaviour to be problematic is in setup-ocaml, which 
is a github action: https://github.com/ocaml/setup-ocaml/pull/915. It applies 
"SHELLOPTS=igncr" globally so that it affects all cygwin bash invocations, 
however, a side effect is that other shell options set via bash option flags cascade down 
into nested bash invocations.

This is something that the setup-ocaml author(s) should fix, perhaps by
making the behavior cygwin-specific.


I'm not sure if this behaviour of SHELLOPTS is intentional, but it is 
definitely confusing particularly from the lens of the given use case.

The behavior of environment variables is well-known and documented. I
don't think SHELLOPTS is a reason to change it.

decide on the correct behaviour of +u versus SHELLOPTS=nounset.

This precdence question is really the only one. Is this issue serious
enough to change previous behavior in an incompatible way?


or
2) Re-export only the initial value of SHELLOPTS to the new environment,

No. That is not consistent with the historical behavior of SHELLOPTS,
and not consistent with the behavior of environment variables.

I'm not sure why the setup-ocaml authors chose the method they did, but
it seems like that's where this issue starts. Maybe they could change
things to run `export -n SHELLOPTS' where appropriate.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to