3

I'm quite new to developing under OSX and assigned some aliases and other preferences in the ~/.profile initially, however it got quite big and messy after a while because I had a lot of Environment Variables to define too.

So I created a ~/.bashrc file (as you would on linux) and placed all my aliases and preferences there. However, since every instance of mac terminal is a login terminal, it doesn't read the .bashrc but just .profile...

So my solution was to place a source .bashrc at the bottom of ~/.profile and that does the job. So I'm wondering whether that is not good and if there is a better and cleaner way of doing that, since this is a bit of a hack?

2 Answers 2

2

See here in this answer from Stack Overflow. Chart below quoted for easy reference.

It's okay to add source ~/.bashrc to ~/.profile.

                     +-----------------+   +------FIRST-------+   +-----------------+
                     |                 |   | ~/.bash_profile  |   |                 |
login shell -------->|  /etc/profile   |-->| ~/.bash_login ------>|  ~/.bashrc      |
                     |                 |   | ~/.profile       |   |                 |
                     +-----------------+   +------------------+   +-----------------+
                     +-----------------+   +-----------------+
                     |                 |   |                 |
interactive shell -->|  ~/.bashrc -------->| /etc/bashrc     |
                     |                 |   |                 |
                     +-----------------+   +-----------------+
                     +-----------------+
                     |                 |
logout shell ------->|  ~/.bash_logout |
                     |                 |
                     +-----------------+
3
  • Oh, that is a very helpful diagram. So if I understood correctly it is actually better to source it from .bash_login? However (since it is FIRST) would it reach my .profile, to instantiate my env vars? Commented Dec 19, 2014 at 12:16
  • @G.Rassovsky Actually I haven't used .bash_login myself. .profile won't get reached if there exists .bash_login. Maybe it's better to use ~/.profile (in my opinion). Commented Dec 19, 2014 at 12:53
  • ah yes, i think you're actually right, i didn't spot this straight away... so best just keep my source .bashrc in .profile and not worry too much about it. :) Commented Dec 19, 2014 at 14:44
2

Most Linux distributions do this out of the box, but you'll want to make sure your .bashrc is still only run by Bash in interactive shells.

Therefore you should guard the inclusion with a line like this:

[ "$BASH_VERSION" ] && [[ $- == *i* ]] || return
. "${HOME}/.bashrc"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.