My .bash_profile
script takes several seconds to run each time I am opening a terminal so this becomes quite annoying.
My testing shows that certain commands are taking a long time so I moved them out of .bash_profile
to a new script .bash_profile_load_in_background
From .bash_profile
I am trying to source in the background.
.bash_profile
# fast stuff here
#.....
# slow stuff here
source .bash_profile_load_in_background & # notice the &
In .bash_profile_load_in_background
I am setting some variables but they are not propogated correctly when the source
is sent to background &
.
here is an abridged version of my "slow" script:
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
if type brew &>/dev/null; then
HOMEBREW_PREFIX="$(brew --prefix)"
if [[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]; then
source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
else
for COMPLETION in "${HOMEBREW_PREFIX}/etc/bash_completion.d/"*; do
[[ -r "$COMPLETION" ]] && source "$COMPLETION"
done
fi
fi
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
# parse the git branch
gb() {
__git_ps1
}
# change the prompt to show working directory and git branch
PS1="\[\e[32m\]\w \[\e[91m\]\$(gb)\[\e[00m\]? "
Easy enough to see that the PS1
prompt isn't changing once the script in the background has completed.
On the other hand if I source the script in "blocking" inline fashion, everything works as expected:
i.e.
Modify
source .bash_profile_load_in_background & # notice the &
to
source .bash_profile_load_in_background # REMOVE THE &
Why is this happening?
Any way to make this work and speed up the .bash_profile
load time in a simple fashion? I don't care if some the functionality takes several seconds to take effect, but it must take effect in the current scope.