0

I wrote some code in .bashrc, that is working as expected.

Here is the code,

if [[ -v X_CHROOT ]]; then
   PATH="/home/user/chroot_local/bin":$PATH
fi

Expectation is that PATH should be appended when I run the chroot command. And is appended as expected.

So, After logging on machine, before chroot, it is,

> echo $PATH
/usr/local/bin/:/usr/local/sbin:......

after chroot, it is,

> echo $PATH
/home/user/chroot_local/bin:/usr/local/bin/:/usr/local/sbin:......

My question is,

Amidst logging, .bashrc already executed.

On chroot command does .bashrc content again execute?

  • I guess that you proved that it can. But you are not telling us how do you enter the chroot. The coreutils' chroot will, by default invoke /bin/sh, which is likely to be bash on your system. If HOME is not changed and is visible there's nothing holding bash from simply finding the .bashrc file. – grochmal Apr 13 '17 at 1:14
  • @grochmal not telling because it is customized tool and I see shell msg after running chroot. Sorry about that – overexchange Apr 13 '17 at 3:54
  • @grochmal Another question is, when you exit from chroot, how does environment change back which is equivalent to undoing .bashrc execution? For example, if .bashrc updates with new PATH on chroot then how does exiting from chroot I see old PATH. How does this work internally? – overexchange Apr 19 '17 at 21:01
  • Each process holds its own environment in the environ global (which sits after the stack on most architectures together with argv). All processes are started by fork+exec and one of the arguments to exec is the environment, which is often taken from the parent (process creating the other process) but does not need to be. In any case, a child process will have its own copy of the environment in its memory space. i.e. all environment inheritance always happen parent->child, and under no circumstance child->parent. – grochmal Apr 20 '17 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.