EOS uses the .bashrc file when the user logs in DE or ssh or telnet or using su. Other distros use a different files. Some distros use ~/.profile and some, very rarely, use ~/.login file. In case of EOS the file ~/.bash_profile calls ~/.bashrc. Also EOS does not define ~/.bash_login file. So in case of EOS everything points to ~/.bashrc file.
Using ~/.profile has certain advantages over using ~/.bashrc. Using ~/.profile allows the user to easily shift shells without in anyway loosing the settings. Also having all the shell startup scripts in a single file actually helps.
So the question is what do you use? Do you use the .profile file? With .bashrc calling .profile ?
OR
Do you call .bashrc from inside the .profile? i.e. reverse setup.
OR
Do you put all the startup scripts, settings inside some other file say ~/.config/profile and invoke that from the .bashrc or .login or .profile file?
OR
Do you do something else? Can you please share?
I am not calling out or saying that one way is better than the rest. Or that something should not be done. Though if there are thoughts regarding those that also would be welcome. This is only to see what alternatives are there and what could be a better approach for certain circumstances.
~/.bash_profile is only sourced during login. For example when you login via SSH. But it is not sourced when you have a GUI session, like KDE or GNOME, and open a terminal. And it is not sourced when a bash script is executed.
~/.bash_login is only sourced during login and only if ~/.bash_profile does not exist.
~/.profile is only sourced during login and only if ~/.bash_profile does not exist and ~/.bash_login does not exist
~/.bashrc is sourced when an interactive shell is opened or a bash script is excecuted. This is the preferred file to add all your customizations (aliases, env variables, functions).
Not many aliases in .bashrc (don’t want to forget basic commands), but lots of scripts in my ~/bin, so I put:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
So .bash_login and .bash_profile are sourced only during login. But not during GUI session? So for example if I am login into my desktop environment (DE) then it is not sourced? From the manual page of bash.
So when we login via a DE then ideally the files, .bash_login, .bash_profile and .profile if they exist, should be invoked. As login via DE should invoke a non-interactive login shell.
The problem with ./bashrc is that it is executed every time, even when a bash script is executed. So that creates unnecessary overheads for execution of a shell script.
only thing i do in .bashrc to have a difference between normal and root user for color and make a seperate file for aliases. copy that file from /etc to your user and done.
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\][\h\[\033[01;36m\] $(pwd)\[\033[01;31m\]]\n>\[\033[00m\] '
else
PS1='\[\033[01;32m\][\u@\h\[\033[01;37m\] $(pwd)\[\033[01;32m\]]\n>\[\033[00m\] '
fi
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
A non-interactive shell is a shell which runs without user input. E.g. executing scripts, cronjobs, executing remote commands like ssh user@host 'cd /tmp; ls -l'
An interactive shell allows user input. E.g. a TTY session (Linux console)
A login shell is a shell with which you login to a computer.
A login shell can be interactive (e.g. TTY session) or non-interactive. A login shell does not inherit an environment from the calling process. A login shell creates its own environment. It is doing that with the various *profile* config files. It is not automatically sourcing ~/.bashrc. If you want a login shell to source ~/.bashrc, you need to source it from within one of the *profile* files.
Correct. su opens an interactive non-login shell. And the new shell inherits the environment from the calling shell.