set MONGODB="/usr/local/mongodb/bin"
This is not a variable assignment. (It is one in C shell (csh, tcsh), but not in Bourne-style shells (sh, ash, bash, ksh, zsh, …).) This is a call to the set
built-in, which sets the positional parameters, i.e. $1
, $2
, etc. Try running this command in a terminal, then echo $1
.
To assign a value to a shell variable, just write
MONGODB="/usr/local/mongodb/bin"
This creates a shell variable (also called a (named) parameter), which you can access with $MONGODB
. The variable remains internal to the shell unless you've exported it with export MONGODB
. If exported, the variable is also visible to all processes started by that shell, through the environment. You can condense the assignment and the export into a single line:
export MONGODB="/usr/local/mongodb/bin"
For what you're doing, there doesn't seem to be a need for MONGODB
outside the script, and PATH
is already exported (once a variable is exported, if you assign a new value, it is reflected in the environment). So you can write:
MONGODB="/usr/local/mongodb/bin"
PATH=${PATH}:${MONGODB}
Best Answer
export
makes a variable something that will be included in child process environments. It does not affect other already existing environments. In general there isn't a way to set a variable in one terminal and have it automatically appear in another terminal, the environment is established for each process on its own.Adding it to your
.profile
makes it so that your environment will be setup to include that new variable each time you log in though. So it's not being exported from one shell to another, but instead is instructing a new shell to include it when it sets up the initial environment.