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

Re: Weird set -u error



On Sun, 28 Aug 2022 at 01:30, Greg Wooledge <greg@wooledge.org> wrote:
> On Sun, Aug 28, 2022 at 12:46:17AM +1000, David wrote:

> > I think there might be one remaining aspect still mysterious, so there
> > might be yet another factor beyond the FIVE you identified ...
> >
> > The fact that this statement, first shown by Tim, does NOT error:
> >
> >   $ ( bash -uc : ; : )
>
> (inside an ssh session)
>
> This is related to how bash optimizes subshell commands when it can.
>
> unicorn:~$ (bash -uc :)
> /etc/bash.bashrc: line 7: PS1: unbound variable
> unicorn:~$ (bash -uc : ; :)
> unicorn:~$
>
> In the first one, there's just one command inside the subshell, so bash
> can replace one of the fork() calls, and treat it like
>
> (exec bash -uc :)
>
> In the second one, there's a second command, so bash can't perform that
> same optimization.
>
> (exec bash -uc : ; :)   would not work the same as   (bash -uc : ; :)
>
> This optimization affects whether the shell_level is reset:
>
> unicorn:~$ (bash -c 'declare -p SHLVL')
> declare -x SHLVL="1"
> unicorn:~$ (bash -c 'declare -p SHLVL'; :)
> declare -x SHLVL="2"
>
> Under normal conditions, this would have no visible effect.  But because
> of the other factors involved, we can see a difference.

Thanks. Is SHLVL something that is useful to understand?
Is there any documentation about it apart from reading the source?
'man bash' is extremely terse about it. But bash exposes it to users,
so I wonder why, what is it useful for?


Reply to: