Bash – Avoid sourcing scripts multiple times

bashbashrcshellshell-scriptterminal

At the moment, when I open a terminal on my Mac, it automatically calls:

source ~/.bash_profile.

What bemuses me, however, is that my shell does not seem to inherit any of the content from ~/.bashrc or ~/.profile. But that might be typical.

I have a number of scripts to inherit using source or .; if I put those calls in ~/.bash_profile, it can take some time to load a new shell window, sometimes 3 or 4 seconds, and that gets old. I imagine that there is some way to source those scripts just once and have my ~/.bash_profile file inherit it somehow.

Note that calling source ~/.bashrc or source ~/.profile from my ~/.bash_profile is not what I want to do, and is also probably a bad idea. It's not what I want to do because it doesn't solve the problem of a slow load for each new shell.

I added some echo statements; every time I open a new bash terminal window, this is logged:

starting to load /etc/profile
finished loading /etc/profile
starting to load bash_profile
finished loading bash_profile

that makes sense, but what is disheartening – do I really need to load these every time a new shell is opened? Why can't it do some fancy inheritance so that we don't have to reload everything every time?

I created a video demo'ing the problem. I have 4 terminal applications I use regularly:

terminal.app                  # misbehaves
iterm2                        # misbehaves
webstorm terminal emulator    # misbehaves
vscode terminal emulator      # behaves!

VSCode actually behaves in the way I would desire. I am guessing that it does this by loading ~/.bash_profile in a parent shell, and only doing that once, when vscode boots up. All of the terminal windows in the app, are then subshells of that one parent shell.

Hopefully this video makes the issue clear:
https://www.useloom.com/share/4e62f0cb24434c4a83b8bd32844b596a

This is a legit issue – most terminal applications do this whole thing wrong, however, quite miraculously, it appears that VSCode from Microsoft actually does this whole thing the right way, see this issue:

https://youtrack.jetbrains.com/issue/WEB-31390

Best Answer

This morning I moved a bunch of personal information out of my bash_profile and bashrc and added them to a new file I call privaterc.

In my privaterc file I set this variable:

PRIVATERC_RUN=yes 

Now in my bash_profile I have added this line:

[[ $PRIVATERC_RUN != yes && -f ~/.privaterc ]] && source ~/.privaterc

This will cause privaterc to only be sourced if it hasn't previously been sourced in this shell.


Also as for the profile weirdness you are seeing, the article I linked in the comment states the following about mac:

Mac OS X — an exception

An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc. Other GUI terminal emulators may do the same, but most tend not to.

Related Question