Bash Login Files Main Diferences

When configuring users login, you should know the bash login files main diferences. As so, you should ask yourself: is it for a login shell or a subshell?

OK, so what’s a login shell?

A login shell is the first shell you log on to when you reach the server. It does not matter if it is ssh, or in the server itself. If it’s your first login, then it’s a login shell.

And a subshell?

A subshell is a shell generated by the parent shell (the login one).


OK, so what does that have to do with the files described in the title?

Well, is has everything to do with it, because:

  • /etc/profile -> this file configures the system variables for every user that logs in
  • /etc/bashrc -> this file configures the system variables for every subshell created by any user (one way to easily remember the purpose of this file is just to associate “rc” to “recursive”, which means derivated from the original)
  • ~/.bash_profile -> because it is located in HOME (~), it loads the system variables, during login, for the current user. So it’s basically /etc/profile but for the current user only.
  • ~/.bashrc -> because it is located in HOME (~), it loads the system variables for every subshell generated by the current user. So it’s basically /etc/bashrc but for the current user only.

Lets give it a try with ~/.bash_profile and ~/.bashrc!

Login into your server.

Edit the file ~/.bash_profile with a file editor and add this:

FOO="Login shell"

Save and exit the file editor.

Edit the file ~/.bashrc with a file editor and add this:

FOO="Subshell"

Save and exit the file editor.

Logout and login again.

Now type:

$ echo $FOO

Your result should be:

$ Login shell

Now type:

$ bash
$ echo $FOO

What you’ve just did, was start a new instance of bash, which can also be called subshell.

Your result should be:

$ Subshell

Yes, it’s that simple!

Leave a Reply

Your email address will not be published. Required fields are marked *