bash – Difference Between .profile and .bash_profile and Why .profile Might Be Missing

bashprofile

So I'm pretty new to Linux and recently installed Fedora 19 on my netbook. I want to have a deeper understanding of Linux and the CLI so I'm now reading Learn Linux: The Hard Way.

In exercise 3 the author gives the following commands:

  • ls -al
  • cat .profile

I try to follow these instructions but to no avail. There is no .profile file in my user name directory as I can see from ls -al. From what I've learned the .bash_profile and .profile file are practically the same. The only difference I concluded from my research is that .bash_profile gets checked first.

What I don't understand is why are there multiple files for the same function in Linux, why is the Author using .profile and not .bash_profile (if I understand correctly .profile won't be read when the system finds .bash_profile first) and how comes that I don't have the .profile file on my system?

Best Answer

The .profile dates back to the original Bourne shell known as sh. Since the GNU shell bash is (depending on its options) a superset of the Bourne shell, both shells can use the same startup file. That is, provided that only sh commands are put in .profile

For example, alias is a valid built-in command of bash but unknown to sh. Therefore, if you had only a .profile in your home directory and put an alias statement in it, sh would complain. So there is a bash specific file that has shell initialization commands which bash will read if and only if there isn't a .profile file present.

Actually that's a bit of an oversimplification in some installations, and I'm not familiar with Fedora. Under bash, /etc/profile is read by the shell before any files in your home directory. If there is a system wide initialization script it often says something like

if there is a $HOME/.profile:
   source it
elseif bash is my shell and there is a $HOME/.bash_profile:
   source that

Why is that way? An attempt at compatibility across decades of shell dialects. Why is the tutorial written that way? The Bourne shell is not often used much any more and some people don't even know that there is any other Bourne-like shell than bash. Even when the (limited) Bourne syntax is used for greater cross-platform compatibility it is often being run by dash or bash in POSIX compatibility mode. Indeed, the actual Bourne shell source is probably a copyright component of Unix System V which appears to be the property of Novell now but I have no idea what, if anything, they are doing with it.

For the beginning user, use either $HOME/.profile or $HOME/.bash_profile but not both and you'll be fine. Since you already have a .bash_profile work with that because it may have system specific stuff in it that your installation needs.

Related Question