I have observed that only the values of exported variables are appended to the my PATH variable via my Mac OS X .bash_profile
. Whereas, the values of locally set variables cannot. Why can't local variables be appended to the path?
“You've misinterpreted something, but what? Post the code that puzzles you. – Gilles yesterday”
Please consider this snippet, where I set the MONGODB variable as:
set MONGODB="/usr/local/mongodb/bin"
export PATH=${PATH}:${MONGODB}
I source .bash_profile
in terminal.
I see the following echo:
PATH=~/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:
Whereas, if I export MONGODB instead, and source my .bash_profile
, I see the following echo:
PATH=~/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mongodb/bin
Perhaps, using set
is improper?
Best Answer
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, thenecho $1
.To assign a value to a shell variable, just write
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 withexport 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:For what you're doing, there doesn't seem to be a need for
MONGODB
outside the script, andPATH
is already exported (once a variable is exported, if you assign a new value, it is reflected in the environment). So you can write: