[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Weird set -u error



On Sat, 27 Aug 2022 at 15:07, <tomas@tuxteam.de> wrote:
> On Sat, Aug 27, 2022 at 11:22:09AM +1000, David wrote:
> > On Sat, 27 Aug 2022 at 10:27, Greg Wooledge <greg@wooledge.org> wrote:

> > > Has anyone managed to reproduce the OP's results, either getting
> > > "interactive" from a bash -c call, or seeing *any* evidence that
> > > /etc/bash.bashrc or ~/.bashrc is sourced from a bash -c call?

> > On Debian 11, when I create a test user, login on a console as that
> > user, and duplicate the recipe provided in the original message[1],
> > the reported problem does NOT occur:

> Aha...

> so main suspects are now ~/.bash_profile ~/.inputrc or some exported
> environment lying around...

On Sat, 27 Aug 2022 at 08:54, Tim Woodall <debianuser@woodall.me.uk> wrote:
> On Fri, 26 Aug 2022, tomas@tuxteam.de wrote:
> > On Fri, Aug 26, 2022 at 10:57:44AM -0400, Greg Wooledge wrote:

> >> There are also various hacks that are compiled into Debian's version
> >> of bash [...]

> >> At this moment, I'm kind of leaning toward one of those hacks being
> >> triggered by your specific combination of factors.

> > That's the most convincing conjecture so far, I'd say :)

> It's bash:

> $ bash -uc :
> $ ( bash -uc : )
> 'PS1='
> /etc/bash.bashrc: line 8: PS1: unbound variable
> $ ( bash -uc : ; : )

I am still thinking about this weirdness, as a learning exercise :)

Some observations and conjecture:

1. No-one except Tim is yet able to reproduce the above. Tim has not given
   us enough information to do so.

2. We dont know what shell and version is initially running *before* any of
   the above occurs. That information is essential. It could be 'bash' or
   'dash' or anything else with different behaviour we have not yet
   considered.

3. It looks like the initial shell is being used interactively, and has
   PS1='$ ' which suggests that is not 'bash', possibly 'dash'.

4. The ':' command is a shell builtin, not an external command.  That could
   be a relevant fact in the above example.

5. My 'foo' examples in a previous message demonstrate that bash -c ...  is
   always non-interactive.

6. 'man bash' tells us that bash when started interactively includes the
   steps of: (a) set PS1 (b) source /etc/bash.bashrc

7. But the Makefile example originally given by Tim requires that (b)
   occurs as if (a) never happened.

8. Similarly the above error message requires that (b) occurs as if (a)
   never happened.

9. How is this error possible then, if an interactive shell does both (a)
   and (b) when it starts? What could possibly be unsetting PS1 before
   step (b) occurs. Something that could occur between (a) and (b) could
   be any commands inside a file named in either an ENV or BASH_ENV
   environment variable. We need to know if either of these exist.

10. On the other hand, a non-interactive shell could do (b) without (a) if
    BASH_ENV=/etc/bash.bashrc was set in the environment that it inherits.

11. Regarding this line from above:
        $ ( bash -uc : )

    I could attempt to explain what happens as: the initial shell forks
    a subshell clone of itself because of the parentheses. That subshell
    clone is interactve. It somehow has PS1 unset, and stupidly sources
    /etc/bash.bashrc which gives the error. The subshell then runs
    'bash -uc :' as a non-interactive shell.

    That might make sense, except for the fact that when I run
    that example here, I do not see any errors!

    Of course I don't want to see errors, but the lack of them indicates
    that my shell and everyone elses shell don't work in the way that my
    explanation says they do.

    Perhaps, whatever strange shell Tim has does source /etc/bash.bashrc
    when it forks a subshell environment, but my shell does not. I don't
    see a good reason why any subshell would source that file again,
    because its environment is supposed to be a clone of its parent.

12. And regarding this line from above:
        $ ( bash -uc : ; : )

    Maybe Tim's strange shell does not error on this line, because
    the command is not a simple builtin. I have no clue.

Finally, to take this any further, I think we need to know:

    What the initial shell is.
    The output of 'env' in the initial shell.
    (particularly any ENV or BASH_ENV values)
    The output of 'cat ~/.bashrc'


Reply to: