Explain this command: . ~/nvm/nvm.sh
Question
I am by no means a novice user on Linux, but I just don't understand why one has to put . in front of this command:
. ~/nvm/nvm.sh
For those in the know, this is how to activate the nvm bash script (it allows for a virtual environment in the NodeJS universe). But if one does not put that starting period in front of the command, then things don't work out. As far as I know, the "." means current directory. Yet if I do this:
cd ~/nvm
nvm.sh
or this
~/nvm/nvm.sh
It will not work. Why? Why must one put "." and then a space before running this command.
Solution
. ~/nvm/nvm.sh
It asks the interpreter to interpret the script in the current process. In bash
it's equivalent to:
source ~/nvm/nvm.sh
You need to execute a script in the current process if you want it to change the environment (variables, et al). You can view more details with help .
~/nvm/nvm.sh
This one actually runs the script in a new bash process. It cannot change the environment of the parent, for example it cannot export variables to the parent process.
OTHER TIPS
The .
means run in the current shell so environment variables you've set or are set in the script are shared. I would wager launching a new shell for the script puts it in an environment it cannot run in.