20

I want to run an NVM command from bash script i.e. nvm use 0.12.7. So, I have written in bash file:

#!/bin/bash
. ~/.nvm/nvm.sh
nvm use 0.12.7

And then run the command in the terminal as sudo ./script.sh (script.sh is my bash file where above code is written). It gives me the result Now using node v0.12.7. But when I check was the version activated or not, I found no affect. i.e. I ran command nvm ls and found the result as:

v0.12.0
v0.12.7

That's mean version 0.12.7 was not being activated. So, which things should I write in bash script as I can active node version from bash file.

8
  • 1
    I'm not great with linux's sudo, but I assume it's because it's setting the node version for the super user, not the current user? Can you run the script without sudo? Commented Dec 22, 2015 at 4:16
  • Yes I did but got same result. Commented Dec 22, 2015 at 4:17
  • The only other reason which I can think of, is that it sets the nvm version within the sub-shell. No idea how to fix that Commented Dec 22, 2015 at 4:22
  • I think it is because you are setting the value for the shell session in the script. Not the node version used in your current terminal. Try using the command in the answer from this post: stackoverflow.com/questions/24585261/… nvm alias default 0.12.7 Commented Dec 22, 2015 at 4:51
  • How about ~/.nvm/nvm.sh && nvm use 0.12.7 from the current shell Commented Dec 22, 2015 at 6:12

1 Answer 1

23

One of the advantages of nvm is that you don't need to use sudo to install versions or to switch to another version. I'm not sure why you are using sudo in your nvm command.

The problem, as others have also said, is that the version is changed in a sub-shell. So the version in your "real" shell is not changed.

You can accomplish this by running your script with . (dot space) in front of it. That will make the script to be able to change stuff in your current shell.

This is my ~/bin/nvm-use-4 script:

. /usr/local/opt/nvm/nvm.sh
nvm use 4

And using it:

prawie:~$ nvm current
v0.10.29
prawie:~$ . nvm-use-4
Now using node v4.2.1
prawie:~$ nvm current
v4.2.1

If you are forced to use sudo here, I don't think it's possible to accomplish what you want, because the sudo'ed command is run in a sub-shell.

Unfortunately, you have not told use why you want to do this or what you want to accomplish. There could be better solutions to solve your problem. For example, if you want to always use a specific version of node.js when you open a new shell, you could add the following line to .profile, .bashrc or equivalent file:

nvm use 0.12.7
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your variety of information. In terms of sudo, I tried in both with or without sudo. But it wasn't working althoguh I mentioned in the question with sudo. I can see that if I use other commands of nvm and run script.sh, it works but activating node version is not working through shell script. i.e. nvm use 0.12.0 or something like that.
It should, if you run . script.sh, with a dot and a space at the start. Does that help?
it's just awesome. It really works. I knew a new thing today. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.