I have my .bashrc and .bash_profile being loaded at shell start; except for some export and alias, there's a command that needs a couple of seconds to run:

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

And I need to wait this 2 seconds delay all the times I open a new terminal.

Given that I almost never use NVM and that I don't need it to be ready immediately after shell loading,

  • how can I delay the execution of that command? (e.g. running 10 seconds after the shell is loaded)
  • how can I set the execution of such command to be asynchronous so that it doesn't block the fast loading and readiness of my shell?
  • Doesn't appear to work. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &>/dev/null still blocks the shell and I can't use it until it finished executing that command. – Kamafeather Jul 10 '19 at 10:13
  • 1
    Try putting parentheses around the command like so ([ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh") – DavidPostill Jul 10 '19 at 10:14
  • 2
    You source a shell script, expecting it to set environment variables. It must be very tricky getting this to work asynchronously ... I would love to learn of a solution. I would expect it's impossible. – Edward Jul 10 '19 at 11:24
  • 2
    Another solution would be to lazy-load that script, provided that there are only a limited number of commands that could possibly need the environment variables it sets. That is, you replace the real commands with shell aliases that load the script behind the scenes. Am I talking sense? – Edward Jul 10 '19 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.