Wednesday, September 14, 2011

OSx and the bashrc

A couple of months ago I purchased my first Mac, a 13" Macbook Pro.  I have to say that I am absolutely loving so far.  The interface is pretty sweet, but for me the best part is that its got a *nix back end.  Learning a new environment like OSx after coming from the Linux world is not a difficult transition, but it certainly has its little speed bumps.

This evening was one such speed bump when I decided to add some things and customize my .bashrc.  The first thing I noticed….. there wasn't a .bashrc in existence yet on the system.  This simply told me that it was pulling its initial settings from /etc/profile (which in turn sources /etc/bashrc on OSx Lion).

So, I went and created my own .bashrc in my home directory, added my settings that I liked to have and copied some of the stuff from the default /etc/bashrc file.  I then saved the file and sourced it with ". ./.bashrc" to test the settings and ensure I didn't break anything.  Cool, all looked to be fine.  I closed the window, opened another one and that is when I noticed that the changes I made were non-existent.

So, I did a little bit of Google digging and quickly learned that by default, OSx looks for the .bash_profile, not the .bashrc file.  That is a bit of a change from Linux as it does look for the .bashrc.  To not look for it is a bit ludicrous in my opinion (as rc files are the most common settings files in the *nix world), but this is not a standard *nix platform.  So, I did what any geek would do and created a soft link for .bash_profile that pointed to my .bashrc that I had just created.  Yes, I just created it and was going to be damned if Apple was going to tell me I couldn't use it.  Call me stubborn, but its my environment, not theirs.

So, after that little speed bump, I need only edit my .bashrc and resource to have my changes take effect.  Now, on to see what other funky differences I can discover.

2 comments:

Tom Loredo said...

It's not correct that the OS X behavior differs from Linux. You need to learn the difference between a login shell and a non-login interactive shell. Terminal runs a login shell, which reads .bash_profile or .profile, not .bashrc. The correct way to get your desired behavior is to create a .bash_profile that defines the behavior you want unique to login shells, and at its end to have it source your .bashrc.

Roughly speaking, you probably want to have settings such as your PATH and other environment variables in the profile, and command aliases and definitions in your .bashrc.

A number of *exchange posts discuss this, e.g.:
profile-and-bashrc-doesnt-work-on-my-mac.

I know this is an old post, but I thought it was worth clarifying since others may find it.

Numberwhun said...

Thanks for the input Tom, I appreciate it. I have been working on Linux/Unix for about 15 years now and honestly have not really separated my shell information into the different files as you mentioned.

When I started ages ago I was taught to create my rc file and put all of my configuration information into there. I am so used to putting my settings into the rc file that it would be quite the change to separate it. I do keep it organized, separating information and making notes with comments (as is proper) so I know what is for what.

I will have to read the link you provided and do some reading on my own to learn the proper way things are handled. I guess its just complacent laziness that I thought I already knew, but hey, I am not above admitting I was wrong and learning the correct information. :)

Regards,

Jeff

 
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.