Bash startup files in OS X

From the bash man page:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

Mac OS X is unusual in that it runs each terminal session as a login shell, most other versions of unix, linux and many GUI terminal emulators do not run as login shells.

If you want to put startup PATH and common settings in ~/.bashrc either because you have a GUI terminal, or just to be consistent with other OS's, then source (load) .bashrc from .bash_profile by adding the following lines to .bash_profile:

if [ -f ~/.bashrc ]; then
source ~/.bashrc

Now when you login to your machine from a console .bashrc will be called.

The alternative is simply to put everything into .bash_profile if you are only running the OS X terminal then every new window/tab that you open will load .bash_profile

On a brand new user account, none of these files will exist, they can be created with any suitable text editor (BBedit/Text Wrangler) that is capable of creating plain text files with unix style (LF) line endings. Save them into your home folder (~/)

Other startup files


Adding aliases to a separate file has exactly the same affect as putting the aliases in .bashrc the advantage of .bash_aliases is that having all your aliases in one file makes it easier to re-load them when you make changes.

Add the following to .bashrc so that .bash_aliases is always loaded:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases

The command . ~/.bash_aliases will source (load) ~/.bash_aliases in the context of the currently running shell.

Hanye - example .bash_aliases
bashrc_dispatch - use symlinks to reorganise .bashrc, .bash_profile and .profile
alias - Create an alias
OS X Syntax

© Copyright 1999-2016
Some rights reserved