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

时间:2015-11-04 11:17:21

标签: nvm

当我从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”中我需要做什么,以便在运行“./test”后“node -v”会给我0.10.40?

请注意,如果我打开一个新终端,并输入“node -v”,我会得到0.10.40。但由于某种原因,在我执行“测试”脚本的shell中,我似乎陷入了4.1.1。

1 个答案:

答案 0 :(得分:1)

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

但正如您的脚本使用handler1 = func(e error) { panic(e) } handler2 = func(e error) { log.Fatal(e) } func foobar(str string) err { _, err := ioutil.ReadAll(resp.Body) checkError(err, handler2) } 来源nvm一样,您可以获取脚本,该脚本将在父shell的上下文中执行:

. ~/.nvm/nvm.sh