What do you use and configure? .bashrc or .profile or .login or .bash_login or something else

This is with reference to the discussion regarding barshrc file.

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.

I put everything in .profile and source it in all other relevant places.

And the .profile file is kept in the $HOME directory?

I’m lazy and used to using .bashrc for so many years… keepin’ it this way.

1 Like

~/.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).

3 Likes

I like to keep .bashrc minimal and tidy so I have a ~/.config/bashrc/alias.txt that I source in .bashrc with source $HOME/.config/bashrc/alias.txt

1 Like

Yep, that’s the default location and I haven’t bothered to try and move it.

I have a half dozen alias in .bashrc

That’s about it.

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.

None of the aliases are defined in the file ~/.bash_aliases? From the documentation it seems that .bash_aliases is not a BASH defined file.

I typically end up using the file ~/.bash_aliases. But that is a neat trick to use a text file inside ~/.config directory.

1 Like

I put them here. Works great, and haven’t had an issue with it yet. (Note this cpu is debian)

That is correct. You can easily test this. In your ~/.bash_profile you can set a “tracker variable” like

export MY_BASH_PROFILE=1

When you then login to gnome/kde/xfce/etc. and open a terminal you can check if that variable is set:

env | grep MY_BASH_PROFILE

It is not set in this case.

No.

A login shell is invoked when you login to a shell with password etc. Like in a ssh session or when you use the linux console.

This implies that if i use the command su userb then the shell will not be a login non-interactive shell. Is this correct?

So in the case of a login via a desktop environment, i.e. DE, how come ~/.bashrc gets invoked?

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
1 Like

Lets be clear with the naming:

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.

1 Like

If you want to learn more about this and other commandline stuff I recommend to ask perplexity:

https://www.perplexity.ai/

Example Questions:

When does bash source the ~/.profile ?
What is a login shell?
Does the su command open a login shell?

All these questions are correctly answered by perplexity including links to the source of the information.

1 Like

This is a good suggestion. Thanks @stormschip

2 Likes