help-bash
[Top][All Lists]
Advanced

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

So confused: bash vs ssh and SHLVL=0?


From: Paul Smith
Subject: So confused: bash vs ssh and SHLVL=0?
Date: Sat, 10 Oct 2020 16:43:05 -0400
User-agent: Evolution 3.36.4-0ubuntu1

I'm running bash 5.0.17(1)-release (x86_64-pc-linux-gnu) on Ubuntu
20.04 GNU/Linux

I'm seeing strange behavior with a combination of ssh and bash and
SHLVL.

For example if I add an "echo bashrc" into my ~/.bashrc and I do this:

  me@local$ ssh remote

  me@remote$ /bin/bash -c 'echo hi'
  hi

it is not sourcing ~/.bashrc, as you'd expect, but if I do this:

  me@local$ ssh remote

  my@remote$ echo 'all:; echo hi' | make -f- SHELL=/bin/bash
  echo hi
  bashrc
  hi

it DOES source my ~/.bashrc!!  I've tried this on other remote systems
running older versions of GNU/Linux and bash and it doesn't behave like
this.

After a lot of banging my head I discovered some mailing list threads
that explained that bash tests SHLVL to decide whether to source
~/.bashrc if SSH_CLIENT is set.

This info would be great to add to the man page, which simply talks
about "being run with its standard input connected to a network
connection", which I don't really understand... how can bash know that?

Anyway, my login shell is definitely bash, and my local SHLVL is 1, and
if I invoke bash again my SHLVL is 2 as expected:

  $ echo $SHLVL
  1

  $ /bin/bash -c 'echo $SHLVL'
  2

However, the problem is that if I invoke a NON-shell, SHLVL is
DECREMENTED and set back to 0 in that process's environment!!

  $ echo $SHLVL
  1

  $ env | grep SHLVL
  SHLVL=0

  $ echo 'all:;echo SHLVL=$(SHLVL) SHLVL=$$SHLVL' | make -f- SHELL=/bin/bash
  SHLVL=0
  SHLVL=1

what is happening here...!!!  It seems to always decrement by 1:

  $ /bin/bash

  $ echo $SHLVL
  2

  $ env | grep SHLVL
  SHLVL=1

Can anyone explain how or why bash decides to decrement SHLVL when
invoking a process?  This is clearly not good for my build environment
as it causes all my make recipes to have ~/.bashrc sourced, which
resets critical environment variables etc.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]