1

I have been learning about the startup scripts in Ubuntu that get run when you open a new shell: /etc/profile, ~/.bash_profile, ~/.profile, ~/.bash_login for login shells and /etc/bash.bashrc, ~/.bashrc for non-login shells. I have a computer that runs Ubuntu, and the order in which things run on that machine fits that order.

However, the files and the order they run in seems different for MacOS. I see that there is /etc/bash instead of /etc/bash.bashrc. In the home directory, I don't see ~/.profile or ~/.bash_login. Weirdest of all, when I put echo statements in all of these files, I see that I'm hitting both /etc/profile and /etc/bash whenever I open Terminal.app. Shouldn't I only hit the first one, seeing as I'm in a login shell? See below, the first three lines are my echo statements, and the fourth is me confirming that I'm in a login shell:

In /etc/profile
In /etc/bashrc
In ~/.bash_profile for user: davidkennell
Davids-MacBook-Pro:~ davidkennell$ shopt login_shell
login_shell     on

So far, I'm coming to the conclusion that the shell startup scripts for Mac are simply completely different from Ubuntu, despite both of them being Unix-based. Where can I get some info on which files are run when initializing login/non-login shells on Mac, and in what order?

3
  • 1
    Note that more correctly this is in, specifically, the Bourne Again shell on MacOS. Like on other operating systems, there are multiple shells available, and it is common for people to use the Z shell instead of the Bourne Again shell on MacOS, which would be a rather different question.
    – JdeBP
    Jul 8 '20 at 8:44
  • The bash manual is correct, but you have to read each startup file to see what files they include. Jul 8 '20 at 12:34
  • @JdeBP Ah, I didn't realize that different shells used completely different initialization scripts - I was under the impression that they all used the same first one or two startup files, which then sourced other files specific to the shells. I see that this was wrong now, and am now looking at shell-specific docs. This clears things up a lot. Jul 8 '20 at 22:50
2

This is (mostly) described in the Bash manual's section on Startup Files.

In brief:

  • /etc/profile is sourced by login shells

  • Then, the first available of ~/.bash_profile, ~/.bash_login, and ~/.profile is sourced.

    There is no ~/.bash_profile or ~/.bash_login by default in Ubuntu, so ~/.profile is sourced. Ubuntu's (well, Debian's) ~/.profile sources ~/.bashrc when the shell is bash:

    # if running bash
    if [ -n "$BASH_VERSION" ]; then
        # include .bashrc if it exists
        if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
        fi
    fi
    

    But this is not generally true for ~/.profile in other distributions or OSs.

  • Debian (and hence Ubuntu) uses a special option to enable sourcing of /etc/bash.bashrc. This is not used in macOS. Instead, at least in Catalina, /etc/profile sources /etc/bashrc (much like Ubuntu's ~/.profile):

    # cat /etc/profile
    # System-wide .profile for sh(1)
    
    if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
    fi
    
    if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
    fi
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.