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


But, while this does sort of work, they are sourced in sub-shells instead of the current shell's context:


foo_bar() {
   echo "hi"


source ./my_file_1.sh &


# zsh: command not found: foo_bar

Is there a way to source files in the background or in parallel while still having them modify the current shell context?

  • 1
    Somewhat similar question for Bash: source a long running script in background from 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. Commented Sep 13, 2023 at 21:39
  • XY problem call-out appreciated. My particular use-case is with a work laptop that has some nasty monitoring tools installed on it that slow everything down. The real solution would be to not use those tools, but that's out of my control. Starting up my shell takes at least 30 seconds, sometimes closer to 2 min, and a similar shell setup on a similar machine without company spy-ware takes, at most, 10 seconds. I'll look through your answer and see if I can implement it to speed up shell startup.
    – Jack
    Commented Sep 14, 2023 at 5:00


You must log in to answer this question.

Browse other questions tagged .