I am new to the Unix/Linux world and could do with some help regarding environment variables, shell scripts and the like. As I understand it if I have the following in a shell script:
MYVAR=somevalue
export MYVAR
then $MYVAR is available within the remainder of the script and to any commands called within it, but not to the shell that called the script. This thwarted my naive plan to create a script which set up a bunch of environment variables, which could then be called from other scripts as required :(. Is there a simple way to achieve what I was trying to do.
If you have a script that you’d like to run in a new subshell (so that when it ends, your current environment is unchanged) you do what you did in the OP. If you want to run the lines of the script in your current shell, thereby modifying its environment, you have to “source” the script, like so:
contents of blah.sh:
export MYVAR="foobar"
export MYOTHERVAR=65
Then at your prompt:
myprompt> source blah.sh
Bash has an alias for “source”, namely the period (.), so you could type “. blah.sh” instead of “source blah.sh”. Tcsh doesn’t have this. To find out what shell you are running, type “ps” and look for “bash” or “tcsh”. (It is possible but unlikely that you are running a shell other than these.)