0

When I source nvm.sh, nvm command becomes available, but not in subshells. All solutions I could find rely on either of the following:

  • adding lines to a profile file located at some more-or-less-hardcoded-into-bash-binary location like ~/.profile
  • adding extra parameters to every invocation of bash that is to become a subshell in which nvm should be available
  • manually sourcing nvm.sh in every subshell where nvm is needed

Is there a solution that relies on neither?

Basically, I want a script like this:

myshell.sh

#!/usr/bin/env bash
magicmagicmagic
bash sorcerysorcery

When I execute that script, I should (with the last bash command) enter a shell in which:

  • nvm command is available,
  • nvm command will be available in every subshell spawned
  • nvm command will be available inside every bash script executed
  • nvm command will be available inside every subshell spawned inside every bash script executed, recursively

This bliss should be effected entirely by magicmagicmagic and sorcerysorcery, and it should not require me to add anything to any "standard" bash profile files. In particular, I do NOT want nvm.sh automatically sourced in my regular do-everyday-tasks shell.

Is this doable?

Szczepan Hołyszewski
  • 2,240
  • 1
  • 21
  • 35
  • Look at `/etc/profile` and `/etc/profile.d/*`. – Nic3500 Mar 11 '22 at 03:08
  • @Nic3500 No. That directly violates the specification in my question. The solution must be fully contained. It must not depend on modifying any system-wide OR user-global stuff. 100% of the magic must happen in `myshell.sh` and possibly in files that will be created solely for this purpose and will exist under a dedicated prefix. – Szczepan Hołyszewski Mar 11 '22 at 10:07
  • The reason why the solution needs to be fully contained is that it must be _redistributable_. I need to be able to tell the other guy "export one environment variable and run `myshell.sh`", not "spend two days learning the ins and outs of profile sourcing in bash, then decide what vital files to modify, then remember to unmodify them when you're done for the day". – Szczepan Hołyszewski Mar 11 '22 at 10:18
  • Would https://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i/50673471#50673471 be acceptable? – Jeff Schaller Mar 11 '22 at 15:32
  • @JeffSchaller no, not really. After further digging, the problem (or feature) is that `nvm` is not a binary, it's a shell function, and therefore the script that defines it **must** be sourced in every shell where you want to use `nvm`. This could be done with some `.bashrc`-like mechanism, but I don't want to hardcode a specific "instance" of nvm into my `~/.bashrc`. Perhaps I can code something in `~/.bashrc` that will load the nvm "instance" specified by an env variable or do nothing if the variable is not set. That would be acceptable, and I'll post it as an answer if I find that it works. – Szczepan Hołyszewski Mar 12 '22 at 09:58

0 Answers0