Shell agnostic startup flow - env, profile, rc, login The goal of these files is to have some common behavior between Bash and Zsh
when starting them. As is known, when Bash starts as a login shell it will
source the first found of the following in your home director: .bash_profile,
.bash_login, or .profile. If Bash is a nonlogin interactive it will source
.bashrc. Meanwhile, Zsh behaves pretty differently. First, it always sources
.zshev. Next, if it's a login shell it will source .zprofile followed by
.zshrc (it sources this even if it's just interactive) and finally .zlogin.
I wanted there to be some common behavior here, so I made it so the flow for
both are:


Common environment
Shell specific environment
Common profile (if login)
Shell specific profile (if login)
Common "rc" (if interactive)
Shell specific "rc" (if interactive)
Common login (if login)
Shell specific login (if login)


The new files I have had to add are .bash_env, .common_env, .common_login,
and .common_rc.

A lot of these files only have some debug statements at the top (which all of
them do), so I won't be posting those. In addition, all of the Zsh files are the
same pattern so I'll only be posting one. I'm not including any of the "actual"
things these do (like set aliases or environment variables) because they're not
relevant to what I want reviewed.

.bash_profile

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "* In .bash_profile"

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_env if it exists"
[ -f "${HOME}/.common_env" ] &amp;&amp; source ${HOME}/.common_env

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.bash_env if it exists"
[ -f "${HOME}/.bash_env" ] &amp;&amp; source ${HOME}/.bash_env

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.profile if it exists"
[ -f "${HOME}/.profile" ] &amp;&amp; source ${HOME}/.profile

# Start of "actual content"

# End of "acutal content"

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_rc if it exists"
[ -f "${HOME}/.common_rc" ] &amp;&amp; source ${HOME}/.common_rc

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.bashrc if it exists"
[ -f "${HOME}/.bashrc" ] &amp;&amp; source ${HOME}/.bashrc

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_login if it exists"
[ -f "${HOME}/.common_login" ] &amp;&amp; source ${HOME}/.common_login

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.bash_login if it exists"
[ -f "${HOME}/.bash_login" ] &amp;&amp; source ${HOME}/.bash_login


.bashrc

This one checks to make sure it's not a login shell before sourcing to avoid sourcing twice.

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "* In .bashrc"

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_env if it exists")
shopt -q login_shell || ([ -f "${HOME}/.common_env" ] &amp;&amp; source ${HOME}/.common_env)

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.bash_env if it exists")
shopt -q login_shell || ([ -f "${HOME}/.bash_env" ] &amp;&amp; source ${HOME}/.bash_env)

[[ "$-" != *i* ]] &amp;&amp; return

shopt -q login_shell || ([ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_rc if it exists")
shopt -q login_shell || ([ -f "${HOME}/.common_rc" ] &amp;&amp; source ${HOME}/.common_rc)

# Start of "actual content"


.zshenv

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "* In .zshenv"

[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_env if it exists"
[ -f "${HOME}/.common_env" ] &amp;&amp; source ${HOME}/.common_env

# Start of "actual content"
These repeated statements are really ugly:


[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "* In .bash_profile"
[ "$MY_STARTUP_DEBUG" = "true" ] &amp;&amp; echo "\t* Sourcing ${HOME}/.common_env if it exists"



You can do better by using a function, for example:

msg() {
    [ "$MY_STARTUP_DEBUG" = true ] || return
    echo "$@"
}

msg "* In .bash_profile"
msg "\t* Sourcing ${HOME}/.common_env if it exists"


Extra to: I removed the double-quotes around true, as that's unnecessary.



Whenever you see repeated logic, try to use a function.
Here's another helper function that can simplify much of the code:

source_if_exists() {
    msg "\t* Sourcing $1 if it exists"
    [ -f "$1" ] &amp;&amp; source "$1"
}

source_if_exists "${HOME}/.common_env"
source_if_exists "${HOME}/.bash_env"


Note that in the original code, you forgot to double-quote the parameters of the source commands.