17

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?

Emiroe
  • 173
  • See also http://unix.stackexchange.com/questions/45684/what-is-the-difference-between-profile-and-bash-profile?rq=1 which answers part of your question. Furthermore, http://unix.stackexchange.com/questions/67940/cron-ignores-variables-defined-in-bashrc-and-bash-profile?rq=1 shows one of the reasons why these various files can lead to confusion for more advanced stuff than you are probably dealing with yet. – msw Jul 20 '13 at 11:48

1 Answers1

19

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.

msw
  • 10,593
  • Thanks! I think I understand it a little bit better now, still a lot to learn I guess. – Emiroe Jul 20 '13 at 11:52
  • A lot of that is historical junk that you really don't need to know; just concentrate on the last paragraph of my answer. Also since you are new here, you can upvote and accept an answer which is the highest form of thanks 'round here. – msw Jul 20 '13 at 11:54
  • Yes, I already accepted your answer. I didn't expect someone to put so much effort in it so I'm very thankful. I accepted your answer but I can't upvote it because it requires 15 reputation and I only have 8. So sorry for that! – Emiroe Jul 20 '13 at 11:57
  • I forgot about the 15 rep limit; right you are. – msw Jul 20 '13 at 11:58
  • 6
    History bonus #1: the true Bourne shell finally escaped from captivity. http://minnie.tuhs.org/ has full source code of V7 (the first unix release with the Bourne shell), and http://heirloom.sourceforge.net/ has a slightly more recent version of the Bourne shell that's still actually usable. History bonus #2: alias is required by POSIX so there aren't many sh around that don't know about it. –  Jul 20 '13 at 17:27