I also make new shells fairly regularly, about half the time because I wan to set some variables that I don't want to have to reset.  Then if/when I forget which window I was in, I can check with SHLVL (at least when using bash, which I normally don't do).  But I'm normally in & out of the sub-shells very quickly so don't often need to check.

Terry


On Mon, Jan 20, 2020 at 8:43 PM Theodore Y. Ts'o <tytso@mit.edu> wrote:
On Mon, Jan 20, 2020 at 09:15:56AM +0100, markus schnalke wrote:
> Hoi.
>
> [2020-01-19 14:22] Grant Taylor via TUHS <tuhs@minnie.tuhs.org>
> >
> > Have you ever used shell level, $SHLVL, in your weekly ~> daily use of Unix?
>
> What's the use of it? The only use of $SHLVL I can think of is the
> answer to the question if ^D will close the last shell or just a
> sub shell. I hardly ever ask myself this question. Probably that
> starts to become relevant when you open sub shells frequently.

<Raises hand>

The normal reason why I'm starting subshells is because I need to
control various environment variables on an ad-hoc basis.  It might be
PYTHONPATH, KRB5CCNAME, GPG_AGENT_INFO, LD_LIBRARY_PATH, or some
combination of the above.  Back when I was regularly using Kerberos
root/admin bits, I had some hard-coded shell aliases to indicate
explicitly I was in a shell that was using my
tytso/root@ATHENA.MIT.EDU or tytso/admin@ATHENA.MIT.EDU kerberos
tickets.

But for ad-hoc use cases, SHLVL is great way to track whether I'm a
non-standard shell environment.  For me, some use case probably comes
up at least week or two.

> With tmux or screen at hand, this use case is obsolete for me.
> (Besides, my shell doesn't know about $SHLVL.)

Before I was using bash regularly, I had hard-coded something like
SHLVL in my .tcshrc.

                                                - Ted