Not understanding when to use ~/.bashrc, /etc/profile ,~/.bash_profile or ~/.profile?

balenshah

Member
Joined
Aug 23, 2022
Messages
74
Reaction score
4
Credits
636
I've read tidbits of pages from around 10 books, and I can safely say I am even more confused than before. Earlier I'd just set everything in .bashrc file. LOL.

Say I've scenarios like these:

1) I want to set system variables for user "jacky", what should I use?

2) I want to set system variables for all users, what should I use?

3) I want to set alias for user "jacky", what should I use?

4) I want to set alias for all users, what should I use?

What are interactive/non-interactive login/non-login shells?

I've asked chatgpt but it gave me convoluted reply. So, it was not very clear.
 


The oracle on bash is the bash man page, which is very extensive but not necessarily easy to negotiate.

Check out the section under the heading "INVOCATION" to get an idea of the order in which bash reads files to get a sense of how it works.

The bash dotfiles do vary in distributions, but bash covers it by trying to read them all. For example debian by default uses a .profile, but some other distros use a .bash_profile for the same purpose.

In the case of logging in from a console terminal rather than a display manager, basically, for a login shell, bash reads .profile (or it's like) once, and thereafter on all non-login shells it just reads .bashrc.

Environment variables and aliases are often placed in .bashrc, and work there. It's possible to run a .bash_aliases file for aliases, and have it called from .profile or .bashrc. Opinions vary on these things.

To your queries:
1. Environment variables for a user can be set in the user's .bashrc
2. System-wide environment variables can be set in /etc/profile.d/
3. User's aliases can go in the user's .bashrc, or user's .bash_aliases if it's configured to be read.
4. For system-wide aliases there are a few things possible some of which are contentious. It's possible to amend the profile and bash files in /etc but messing with these system files is not something everyone is want to do. Writing new config files in /etc/profile.d is probably wiser. Another approach is to amend the dotfiles in /etc/skel so that they already include the aliases one wishes users on the system to use, so that when a new user is added to the system, those aliases are already present and working. It might be worth looking into minimising aliases and used bash functions instead. A number of bash experts advocate taking that route.

For GUI and display manager logins, things can vary from the above, and I'm not familiar enough with them to comment usefully.
 
Last edited:

Members online


Top