[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Odd Behavior: $SHLVL decreases in some situations with a pipe
From: |
Chet Ramey |
Subject: |
Re: Odd Behavior: $SHLVL decreases in some situations with a pipe |
Date: |
Mon, 27 Jun 2022 10:16:41 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 |
On 6/27/22 8:02 AM, H. Thiele wrote:
Bash Version: 5.1
Patch Level: 16
Release Status: release
Description:
In some situations (see Repeat-By) the value of the $SHLVL (Shell Level)
variable may
unexpectedly decrease by 1, e.g. when the stdout output of "printenv" is
redirected to
via a pipe.
As you surmise, the behavior is related to bash optimizing away forks and
using an implicit `exec' in the pipeline. I changed this particular case
back last September as the result of
https://lists.gnu.org/archive/html/help-bash/2020-10/msg00012.html
which contains a pretty good discussion of the issue.
Reddit user Electronic_Youth points out some other, perhaps related issue: if
one sets
SHLVL to a value higher than 999 a warning is generated when a new shell is
started, but
more interesting a warning is generated two times when printenv is used
(perhaps suggesting
some code runs twice?) I reproduced that double warning like this:
$ SHLVL=1001
$ printenv SHLVL | cat
bash: warning: shell level (1000) too high, resetting to 1
bash: warning: shell level (1000) too high, resetting to 1
1
It's slightly related. You get two messages because bash-5.1 tries to
optimize away the exec in both subshells, and notices that SHLVL is too
high when trying to adjust it.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/