Skip to content

Comment on Show HN: A fix for the .bash_profile .bashrc .profile madnessparent

Comments

I've always thought of it as

login/profile file(s)

- Run on a "new" shell when there's not any environment present. It should contain things that are "carried over" from one process to child process (ie, exported variables). May also contain things that are run "only once" from the user's point of view (check mail, yadda yadda)

- You don't want these commands run for every subprocess. Imagine that bash sources /etc/profile on each "new" shell to set PATH. You append to PATH in your user profile to add some additional directories. If the profile was sourced for each child process, your path would grow and grow, containing multiple copies of the same directories.

rc file(s)

- Run for each subshell (with caveats[1]) so that you can define things that aren't carried over to subprocesses (define aliases, functions, etc)

[1] caveats being things like remote shell command execution. I honestly don't know why it's not sourced then, but my guess would be that, if you're running a remote command, you're expected to not need things like aliases and functions defined.

Thinking about it all this way, the way the files works just seems... natural to me.

Alternatively, just ensure sourcing .bash_profile is idempotent[1], then .bashrc can simply be

    if [ -n "$PS1" -a -r "$HOME/.bash_profile" ]; then
        . "$HOME/.bash_profile"
    fi
because, on all platforms, PS1 is non-empty on startup iff bash is interactive.

[1] E.g., assuming PATH starts out non-empty, this

    export PATH_DEFAULT PATH="<stuff>:${PATH_DEFAULT:=$PATH}:<other stuff>"
should work for PATH in bash much as
    if [ -z "$PATH_DEFAULT" ]; then
        export PATH_DEFAULT="$PATH"
    fi
    export PATH="<stuff>:$PATH_DEFAULT:<other stuff>"
works in most any Bourne-derived shell. Having the _DEFAULT variables around comes in handy for clean build environments, as well (e.g., I build Emacs.app once and distribute it to several Macs, so I don't want configure to pull in random dependencies that just happen to be installed in MacPorts on my build machine).
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.