Wednesday, January 18, 2017

Linux: Managing Shell Configuration Files

If you’re using a non-login shell, things are pretty straightforward. The bash shell simply runs /etc/bashrc for system-wide functions and aliases, and then it runs ~/.bashrc from the user’s home directory for user-specific customizations.
If you’re using a login shell, bash first runs /etc/profile and applies the configurations specified in that file. After that, however, things get a little more complex. As you may have noticed, several of the files listed sound like they do exactly the same thing. You’re right, they do. The issue here is that no distribution uses all of these files. For example, a Fedora system uses ~/.bashrc, ~/.bash_profile, and ~/.bash_logout. Alternatively, openSUSE and Ubuntu systems use ~/.bashrc and ~/.profile.
When a login shell is run, the bash shell program searches for configuration files in the following order:
 1.  ~/.bash_profile
 2.  ~/.bash_login
 3.  ~/.profile
It uses the first file it finds and ignores all of the rest. This isn’t much of an issue on SUSE and Fedora. Remember that .bashrc is not read by bash when loading a login shell (although it may be called by .bash_profile or .profile). Therefore, after reading /etc/profile, the bash shell reads .bash_profile on a Fedora system. Likewise, on an openSUSE system bash reads .profile after reading /etc/profile.
The .bash_logout file is used only when you log out of a login shell. Most distributions don’t include this file in users’ home directories by default. However, most distributions do allow individual users to create their own .bash_logout file and customize it with their own commands to be run at logout.

No comments:

Post a Comment