SHELLOPTS environment variable causes shell options to cascade
From:
Tobi Laskowski
Subject:
SHELLOPTS environment variable causes shell options to cascade
Date:
Sat, 4 Jan 2025 19:47:07 +0000
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.
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"
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.
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. Specifically, the `nounset` option applies to places where it
didn't previously apply (which breaks a configure script of libuv). I know
`igncr` only applies to cygwin bash, but it can be substituted for any option
in regular bash and the same behaviour occurs.
To be clear, the intention here was to apply a specific shell option globally
(namely igncr), and the unintentional side effect was that the behaviour of how
other shell options are applied has changed.
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. This
behaviour is not explicitly documented either:
https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-SHELLOPTS.
It seems the solution would be to either:
1) Document this cascading behaviour of SHELLOPTS, and decide on the correct
behaviour of +u versus SHELLOPTS=nounset.
or
2) Re-export only the initial value of SHELLOPTS to the new environment, prior
to modifications due to `set` or bash option flags. I've added a patch below
that achieves this, if it seems reasonable then I will write a test and format
it properly as a patch request.
Regards,
Tobi
---
variables.c | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/variables.c b/variables.c
index 1a0c2c45..ba65b97f 100644
--- a/variables.c
+++ b/variables.c
@@ -177,6 +177,11 @@ char **export_env = (char **)NULL;
static int export_env_index;
static int export_env_size;
+/* For the SHELLOPTS environment variable, we must save and re-export
+ the original value as at initialization, ignoring changes resulting from
+ other methods of modifying shell options. */
+static char *shell_opts_value = (char *)NULL;
+
#if defined (READLINE)
static int winsize_assignment; /* currently assigning to LINES or
COLUMNS */
#endif
@@ -515,7 +520,12 @@ initialize_shell_variables (env, privmode)
temp_var = bind_variable (name, string, 0);
if (temp_var)
{
- VSETATTR (temp_var, (att_exported | att_imported));
+ VSETATTR (temp_var, att_imported);
+ if (STREQ (name, "SHELLOPTS"))
+ shell_opts_value = string;
+ else
+ VSETATTR (temp_var, att_exported);
+
if (ro)
VSETATTR (temp_var, att_readonly);
}
@@ -5129,7 +5139,14 @@ maybe_make_export_env ()
export_env = strvec_resize (export_env, export_env_size);
environ = export_env;
}
- export_env[export_env_index = 0] = (char *)NULL;
+
+ if (shell_opts_value)
+ {
+ export_env[export_env_index = 0] = mk_env_string ("SHELLOPTS",
shell_opts_value, 0);
+ export_env[export_env_index = 1] = (char *)NULL;
+ }
+ else
+ export_env[export_env_index = 0] = (char *)NULL;
/* Make a dummy variable context from the temporary_env, stick it on
the front of shell_variables, call make_var_export_array on the
---