Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the various shell profiles?

What's the difference between ~/.bashrc, ~/.bash_login, ~/.bash_logout, ~/.bash_profile, ~/.profile, /etc/profile, /etc/bash.bashrc, /etc/ssh/ssh_config and sshd_config, when are they loaded and what are their purposes?

like image 371
0x4a6f4672 Avatar asked Jun 16 '11 13:06

0x4a6f4672


People also ask

What is the difference between ETC profile and ~/ bash_profile?

/etc/profile is global configuration for login shells (interactive or not), ~/. bash_profile is per-user configuration for login shells, and ~/. bashrc is configuration for interactive non-login shells.

What is the difference between bashrc and Zshrc?

bashrc is read by Bash, . zshrc by Zsh, so which one is used depends on which shell you're using. Save this answer.

Am I on bash or zsh?

Now, coming to the article's main subject, how will you know that you have bash or zsh? The answer is quite simple. Use the “–version” command to confirm the existence of both shells on your Linux system.

What is a profile in Linux?

profile file contains your individual profile that overrides the variables set in the /etc/profile file. The . profile file is often used to set exported environment variables and terminal modes. You can customize your environment by modifying the . profile file.


2 Answers

The man page for bash says there are the following initialization files for bash shells:

/etc/profile
      The systemwide initialization file, executed for login shells
/etc/bash.bashrc
      The systemwide per-interactive-shell startup file
/etc/bash.bash.logout
      The systemwide login shell cleanup file, executed when a login shell exits
~/.bash_profile
      The personal initialization file, executed for login shells
~/.bashrc
      The individual per-interactive-shell startup file
~/.bash_logout
      The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
      Individual readline initialization file

Apparently there seem to be different configuration files for the different shells (bash, zsh, csh, and others). There seem to be as many shells as different linux and unix versions: csh, ksh, bash, zsh, ... Bash has a .bashrc, Zsh has a .zshrc, etc. One can also distinguish between login shells and non-login shells and between system-wide defaults and user-specific defaults.

It makes sense to distinguish between login and non-login shells, because some commands should be processed only at login, while other commands should run everytime you open a new terminal window. That is the difference between .bash_profile and .bashrc. For bash the .bashrc is reloaded every time you start a new copy of bash, i.e. when you start a new bash but do not login. The .bash_profile or .profile is loaded only when you login. The abbtreviation rc in bashrc stands for "run commands" or "run control" and is a convention adopted from older Unix systems.

system-wide defaults for..

  • /etc/profile ..login shells, for interactive shells with login
  • /etc/bashrc ..non-login Bash shells

user-specific defaults in home directory ~ for..

  • ~/.profile ..login shells, called after login
  • ~/.bashrc ..non-login shells, if already logged in
  • ~/.bash_profile ..login shells, called after login (lower priority)

user-specific defaults in home directory for login and logout

  • ~/.bash_login ..login shells (called upon login)
  • ~/.bash_logout ..login shells (called upon logout)

The following links were helpful: .bashrc vs .bashprofile and .bash_profile vs .bashrc, the bash manual page (man bash), and Zsh/Bash startup files loading order (.bashrc, .zshrc etc.).

like image 63
0x4a6f4672 Avatar answered Oct 05 '22 23:10

0x4a6f4672


I happen to be curious about these files and did some experiment myself. It turns out to be a little different than what is in the documents.

I know the differences between interactive and non-interactive or login and non-login.

I tried on two computers, my macbook pro with OS 10.9 and a server with ubuntu server 13.10. I add the following command into the /etc/profile:

echo "Loading /etc/profile"

And similar commands into /etc/bash.bashrc, /etc/bashrc, /etc/bash.bashrc, ~/.profile, ~/.bash_profile, ~/.bashrc, ~/.bash_login and make sure that these files do not source each other inside themselves.

(OS 10.9, GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)) On the mac, with interactive login bash, I have:

Loading /etc/profile
Loading ~/.bash_profile

Which means that the files loaded directly are only /etc/profile and ~/.bash_profile.

with interactive non-login bash, I have:

Loading ~/.bashrc

which means that the file loaded directly is ~/.bashrc.

(ubuntu server 13.10 GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)) On the ubuntu, with interactive login bash, I have:

Loading /etc/profile
Loading ~/.bash_profile

Which means that the files loaded directly are only /etc/profile and ~/.bash_profile.

with interactive non-login bash, I have:

Loading /etc/bash.bashrc
Loading ~/.bashrc

which means that the files loaded directly are /etc/bash.bashrc and ~/.bashrc.

I do not know why~

like image 34
Archimedes520 Avatar answered Oct 06 '22 00:10

Archimedes520