For various reasons I would like to be able to source a series of files in background and wait for them all to finish:
source ./my_file_1.sh &
source ./my_file_2.sh &
...
# etc
wait
But, while this does sort of work, they are sourced in sub-shells instead of the current shell's context:
my_file_1.sh
:
foo_bar() {
echo "hi"
}
main_source_file.sh
:
source ./my_file_1.sh &
wait
# zsh: command not found: foo_bar
foo_bar
Is there a way to source files in the background or in parallel while still having them modify the current shell context?
bash_profile
. Read the answers there to learn what obstacles you need to overcome. You want to do this with multiple files in parallel, so you cannot just translate my answer to Zsh. But even if you managed to do it without any race condition, is it worth it? Do your actual files perform long-running computations? What is the underlying problem? Beware of XY.