104

I found a lot of people figuring out why instruction similar to this :

Put X to your ~/.bashrc and you can do Y

don't work.

It always turns out that Mac OS X's bash's startup files (or Mac's Bash itself) doesn't source ~/.bashrc file, either in the login shell, or in a shell spawned from window system - like Terminal app in Mac OS X)

Why is this, when all other Unix-like systems with Bash I've worked with before have done this?

PS:

I found what Bash itself says about startup files (which could be helpful) :

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.

Source :http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files

meetar's user avatar
meetar
1901 silver badge6 bronze badges
askedFeb 3, 2014 at 12:31
Paweł Brewczynski's user avatar
1
  • 1
    I also noticed that~/.bash_profile is executed but not~/.bashrc. Wierd.CommentedDec 10, 2016 at 16:03

2 Answers2

96

In OSX the terminal by default starts a login session so reads .bash_profile etc. (The GUI login process that asks for your name and password does not use shell scripts and starts no shell it is all done from launchd and the workspace)

On other Unices xterm runs a non login shell by default so they read .bashrc as the scripts that present you with your password etc at login call the login session and all terminals are sub process of this and inherit the shell environment.

From the GNU document you referred to

Invoked as an interactive non-login shell

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

So, typically, your ~/.bash_profile contains the line

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

after (or before) anylogin-specific initializations.

answeredFeb 3, 2014 at 12:42
mmmmmm's user avatar
3
  • This may have been true at one time, but it's not any longer. First, Apple has changed the default shell frombash tozsh. Second, Apple has put in place two"system-wide"*rc files to configurebash:/etc/bashrc_Apple_Terminal and/etc/bashrc. That they have done all of this with virtually zero documentation is...par for the course.CommentedApr 9, 2023 at 16:27
  • This is still true. Terminal.app starts a login session (so reads zlogin or zprofile) Whilst xterm etc doesn't as the login shell is earlier in the boot phase - systemd migh have chnaged thatCommentedApr 9, 2023 at 16:31
  • No, it's not true any longer - sorry.CommentedApr 9, 2023 at 16:35
20

bash only reads.bashrc fornon-login shells:

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.

As Terminal startsbash as a login shell (runw to see that thebash instances are executed as-bash),.bashrc is never read automatically.

answeredFeb 3, 2014 at 12:41
nohillside's user avatar
2
  • So the point is, that Termina app on Mac OS X always create "login" instance of bash ? Why ? If you really don't login. (You have login earlier from GUI)CommentedFeb 3, 2014 at 12:59
  • 2
    Well it is the first time you run the shell as you so it is a login, in X11 based Unices you start by running the shell which then runs X so it is not a loginCommentedFeb 3, 2014 at 13:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.