0

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?

12
  • 1
    PS1 is not an environment variable - it's a shell variable. Those variables are not inherited from the parent. They are read from the profile / bashrc files every time a new shell starts. The question is if you changed the PS1 variable in the parent, or if it comes from one of your profile / bashrc files.
    – aviro
    Commented Sep 5, 2023 at 14:58
  • 2
    Please edit your question and add more details. What value of PS1 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
    Commented Sep 5, 2023 at 15:04
  • @aviro Shell Command Language section states: "Variables shall be initialized from the environment ..." and lists PS1 as one of the Shell Variables. Commented Sep 5, 2023 at 15:41
  • 1
    @Vilinkameni Yes, If there is an environment variable it would be used instead of the shell variable. So, for instance, if you run export PS1=..., now PS1 will be an environment variable and children bash instances will inherit it. But if you just run PS1=... (without export), it will be shell variable and children bash processes won't inherit it.
    – aviro
    Commented Sep 5, 2023 at 15:50
  • @aviro So they are "inherited from the parent" - if they are exported in the parent. Commented Sep 5, 2023 at 15:54

1 Answer 1

6

First, you need to understand that PS1 is usually a shell variable, which means it isn't inherited by the children. So unless you explicitly ran export PS1=... and made PS1 an environment variable, every new bash process will get the PS1 (and other shell variables) from the rc files and not from the parent. So you first need to find out where exactly your PS1 is defined.

You can verify that by exporting your PS1:

export PS1

And then run your ./bash. You'll see that in that case, PS1 will be inherited by the new shell.


So why doesn't your compiled bash get the PS1 shell variable you expect from the rc files?

Here's my guess: On many systems, PS1 is being defined in /etc/bash.bashrc. But not all bash versions read this file. It depends on how bash was being compiled. A good rule of thumb would be checking your bash man pages. For instance, for Ubuntu you'll see:

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

However, /etc/bash.bashrc is not even mentioned in the bash man page you downloaded from that [git]:

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

Additionally, in the README file of bash from Debian, you'll see:

5. What is /etc/bash.bashrc? It doesn't seem to be documented.

The Debian version of bash is compiled with a special option (-DSYS_BASHRC) that makes bash read /etc/bash.bashrc before ~/.bashrc

So in the git repository you've used, you'll see at config-top.h that the line that defines SYS_BASHRC is commented out by default.

/* System-wide .bashrc file for interactive shells. */
/* #define SYS_BASHRC "/etc/bash.bashrc" */

So your bash as it's built by default won't read /etc/bash.bashrc on startup, and if your PS1 is defined there, it won't get it.

If you remove the comment from this line:

#define SYS_BASHRC "/etc/bash.bashrc"

and run make again, my guess is that your new bash will show the PS1 variable you expect.

13
  • 1
  • Thanks, the only question remaining is why the command listing all sourced files shows that /etc/bash/bashrc is being source when it isn't, I haven't spend much time analyzing this command.
    – Rustacean
    Commented Sep 6, 2023 at 17:11
  • @Rustacean your command is faulty. It lists explicit source commands, but of course misses the files that bash reads initially, and also files sourced using .. Try something like: PS4=' source:${BASH_SOURCE[0]@Q} ' /bin/bash -lixc exit 2>&1 >/dev/null | grep -Po '^ *source:\S*' | awk '!seen[$0]++'
    – muru
    Commented Sep 6, 2023 at 22:30
  • The file that sets PS1 is listed by my command, but for some reason it isn't sourced, that's my problem.
    – Rustacean
    Commented Sep 10, 2023 at 10:20
  • 1
    @Rustacean the command with PS4 will help you find where PS1 is defined. Check the meaning of PS4 in the man pages of bash, and you'll understand how it might help.
    – aviro
    Commented Sep 11, 2023 at 12:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.