如何使脚本中的 nvm 运行 影响调用 shell 的环境?

How to make nvm run from within a script influence the environment of the calling shell?

当我从 shell 脚本中 运行 nvm 时,它似乎不会影响调用环境 shell:

$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1@global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$ 
$ cat test
#!/bin/bash
. ~/.nvm/nvm.sh
nvm use 0.10.40
nvm alias default 0.10.40
echo NVM_PATH=$NVM_PATH
echo MANPATH=$MANPATH
echo PATH=$PATH
echo NVM_BIN=$NVM_BIN

$ ./test
Now using node v0.10.40 (npm v1.4.28)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/home/ubuntu/.nvm/v0.10.40/lib/node
MANPATH=/home/ubuntu/.nvm/v0.10.40/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
PATH=/home/ubuntu/.nvm/v0.10.40/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1@global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_BIN=/home/ubuntu/.nvm/v0.10.40/bin
$ 
$ node -v
v4.1.1
$ env | grep -i node
MANPATH=/home/ubuntu/.nvm/versions/node/v4.1.1/share/man:/usr/local/rvm/rubies/ruby-2.2.1/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
NVM_PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/lib/node
PATH=/home/ubuntu/.nvm/versions/node/v4.1.1/bin:/usr/local/rvm/gems/ruby-2.2.1/bin:/usr/local/rvm/gems/ruby-2.2.1@global/bin:/usr/local/rvm/rubies/ruby-2.2.1/bin:/mnt/shared/bin:/home/ubuntu/workspace/node_modules/.bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/mnt/shared/sbin:/opt/gitl:/opt/go/bin:/mnt/shared/c9/app.nw/bin:/usr/local/rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NODE_PATH=/mnt/shared/lib/node_modules
NVM_BIN=/home/ubuntu/.nvm/versions/node/v4.1.1/bin
$ 

我需要在脚本 "test" 中做什么,以便 "node -v" 在我 运行 "./test" 之后给我 0.10.40?

请注意,如果我打开一个新终端并输入 "node -v",我会得到 0.10.40。但出于某种原因,在我执行 "test" 脚本的 shell 中,我似乎被 4.1.1.

困住了

Bash 脚本 运行 在它们自己的进程上下文中,从 parent 进程继承其环境。无法更改 parent 的环境。参见 Can a shell script set environment variables of the calling shell?

但是正如您的脚本使用 . ~/.nvm/nvm.sh 获取 nvm 一样,您可以获取您的脚本,它将在 parent shell:

的上下文中执行它
$ node -v
v4.1.1
$ ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2@global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v4.1.1
$ source ./test
Now using node v0.10.40 (npm v2.14.8)
default -> 0.10.40 (-> v0.10.40)
NVM_PATH=/Users/william/.nvm/v0.10.40/lib/node
MANPATH=/Users/william/.nvm/v0.10.40/share/man:/Users/william/.rvm/rubies/ruby-2.1.2/share/man:/usr/local/share/man:/usr/share/man:/opt/X11/share/man:/usr/local/MacGPG2/share/man:/Users/william/.rvm/share/man:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/share/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
PATH=/Users/william/.nvm/v0.10.40/bin:/Users/william/.rvm/gems/ruby-2.1.2/bin:/Users/william/.rvm/gems/ruby-2.1.2@global/bin:/Users/william/.rvm/rubies/ruby-2.1.2/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:~/local/bin:~/bin:/Users/william/.rvm/bin:/Users/william/.rvm/bin:./node_modules/.bin:/usr/local/heroku/bin
NVM_BIN=/Users/william/.nvm/v0.10.40/bin
$ node -v
v0.10.40