I ran
git clone https://git.savannah.gnu.org/git/bash.git
cd bash/
./configure
make
./bash
I noticed that the newly launched Bash instance did not inherit the environment, specifically the PS1 variable that defines the shell prompt, from the parent shell.
The inheritance works for /bin/bash
List of sourced files is the same for /bin/bash
and ./bash
./bash -lixc exit 2>&1 | sed -n 's/^+* \(source\|\.\) //p'
/bin/bash -lixc exit 2>&1 | sed -n 's/^+* \(source\|\.\) //p'
Edit:
As aviro mentiond PS1 was defined without export, so when I tried exporting it got inherited, so my initial question was wrong.
On my machine PS1 is defined in two files
/etc/bash/bashrc
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
[[ $DISPLAY ]] && shopt -s checkwinsize
PS1='[\u@\h \W]\$ '
And /etc/bash/bashrc.d/artix.bashrc
if ${use_color} ; then
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\][\h\[\033[01;36m\] \W\[\033[01;31m\]]\$\[\033[00m\] '
else
PS1='\[\033[01;36m\][\u@\h\[\033[01;37m\] \W\[\033[01;36m\]]\$\[\033[00m\] '
fi
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1='\u@\h \W \$ '
else
PS1='\u@\h \w \$ '
fi
fi
When I ran ./bash
the PS1 is \s-\v\$
and I have no idea why.
The command listing all sourced file shows that both of these files should be sourced when run with ./bash
, but for some reason they aren't or shell starts in different type/mode. Why?
PS1
is not an environment variable - it's a shell variable. Those variables are not inherited from the parent. They are read from theprofile
/bashrc
files every time a new shell starts. The question is if you changed thePS1
variable in the parent, or if it comes from one of yourprofile
/bashrc
files. – aviro Sep 05 '23 at 14:58PS1
do you see, what value do you expect? Do you set the variable in a file like.bashrc
? Does the problem not occur if you start/bin/bash
instead of./bash
? – Bodo Sep 05 '23 at 15:04PS1
as one of the Shell Variables. – Vilinkameni Sep 05 '23 at 15:41export PS1=...
, nowPS1
will be an environment variable and childrenbash
instances will inherit it. But if you just runPS1=...
(withoutexport
), it will be shell variable and childrenbash
processes won't inherit it. – aviro Sep 05 '23 at 15:50export
ed in the parent. – Vilinkameni Sep 05 '23 at 15:54PS1
is usually (and possibly in the OP's case) not exported. – Kamil Maciorowski Sep 05 '23 at 16:01./bash
or/bin/bash
will make it run the commands inbashrc
, but notprofile
because it's not a login shell. – Sotto Voce Sep 05 '23 at 16:09PS1
is especially odd in that Bash explicitly clears it if running non-interactively. If nothing else, that's one reason to set it inbashrc
, and it might even be unconditionally set, making it rather hard to inherit the prompt from the environment. But ifbashrc
doesn't set it, and you run an interactive shell, it will get inherited as usual. That can be useful if you want to run a shell in some unusual environment and feed it a prompt to remind yourself of that. But you need to make sure your startup files support that. – ilkkachu Sep 05 '23 at 16:40PS1=inherited XYZ=inherited ./bash
and see what the prompt looks like and whatecho "PS1=$PS1" "XYZ=$XYZ"
print. Same for the other Bash. – ilkkachu Sep 05 '23 at 16:42/bin/bash
" so the problem doesn't occur, but my initial guess was wrong, it is not related to inheritance. – Rustacean Sep 06 '23 at 17:15bash -lix
doesn't tell all files it reads. It tells the.
orsource
commands it executes. If you look at the list it outputs, it's missing/etc/profile
or.profile
, even though Bash does read them for a login shell. And similarly when run without the-l
option, the list is missing~/.bashrc
. That is, it doesn't tell the files it runs by itself, meaning it doesn't tell the options it was compiled with. – ilkkachu Sep 06 '23 at 17:37strings ./bash |grep bashrc
and compare with the other binary. Or put something in/etc/bash/bashrc
you can use to tell if it was actually read, perhaps something likeecho this is /etc/bash/bashrc
or whatever. Just be sure to remove it later. – ilkkachu Sep 06 '23 at 17:41