Re: Weird set -u error
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.
Reply to: