The three files are read in this order?
.bash_profile .profile .bashrc
This is not happening when I first open a terminal.
I have trace statements in the files appending to the file init.log. Please take a look at the following. It begins after opening a terminal. I have placed a comment to show where the log picks up after the su command.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="314245544159545f7155545358505f">[email protected]</a>:~$ cat init.log reading .bashrc done reading .bashrc <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="314245544159545f7155545358505f">[email protected]</a>:~$ su - stephen Password: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a2a5b4a1b9b4bf91b5b4b3b8b0bf">[email protected]</a>:~$ cat init.log reading .bashrc done reading .bashrc # # after su # reading .bash_profile reading .profile reading .bashrc done reading .bashrc done reading .profile done reading .bash_profile <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44373021342c212a042021262d252a">[email protected]</a>:~$
So the su – login triggers the expected sequence however the initial login reads only the bashrc. This cannot be correct. Can someone explain under what conditions this would occur. I could modify the bashrc and profile files so that the initial read includes all expected files but I would rather get to the root of the problem and fix it there.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The answer is that bash will look for these three files (in slightly different situations) but will typically only execute one of them.
When running a login shell (typically when you log in on a terminal, or when you open a GNOME Terminal or similar, or when you use su -), more specifically an interactive login shell, then it will execute the system-wide /etc/profile and after that’s done, it will look for ~/.bash_profile, ~/.bash_login or ~/.profile and execute the first of those that it finds.
From the bash man page:
When bash is invoked as an interactive login shell, or as a
non-interactive shell with the--loginoption, it first reads and
executes commands from the file/etc/profile, if that file exists.
After reading that file, it looks for~/.bash_profile,~/.bash_login,
and~/.profile, in that order, and reads and executes commands from
the first one that exists and is readable. The--noprofileoption may
be used when the shell is started to inhibit this behavior.
When bash is executed as an interactive shell, more specifically an interactive non-login shell, then it will read ~/.bashrc and execute that file.
From the bash man page:
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--norcoption. The--rcfile
file option will force bash to read and execute commands from file
instead of~/.bashrc.
What Linux distributions usually do is ship ~/.bash_profile, ~/.profile and ~/.bashrc files that chain each other, so that you have more consistent behavior without having to duplicate settings between the files…
For instance, Debian’s default ~/.profile contains this snippet:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
So it’s explicitly sourcing ~/.bashrc, so that both login and non-login interactive shells will all include the customizations added to that file.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0