Setting environment variables
Up to this point, we have talked about defining shell variables and then using them in later command lines. You can also define a shell variable and then call a shell script that makes use of that variable. But you have to do a certain amount of preparation first.
A shell script is run as a child process to the parent shell. By default, the child process does not share any variables with the parent. If you define a variable var in the parent shell, it is local to the current session; any shell script, or child process, that you call will not inherit var.
setenv var [value]The setenv command
says that you want the variable var passed on to all the child
processes that you execute in this session. After you do this, var becomes
inherited and the variable is known to all the commands and shell
scripts that you use.setenv myname "Friar Tuck"
Now all your child processes can use the myname variable
to obtain the associated name. You may, for example, have shell scripts
that write form letters that contain your name, Friar Tuck,
obtained from the myname variable. When a script or child process begins running, it automatically inherits all the environment variables passed on to it. However, if the script changes the value of one of those variables, that change is not passed back to the parent process —unless you run the script with the source utility.
setenv name [value]indicates
that the variable with the given name should
be defined as an environment variable. When other programs are run
from that script, they inherit the value of all environment variables.
However, when the script ends, all its environment variables are lost
to the calling shell.Some variables are automatically inherited by the software that creates them. For example, if you invoke the shell, the initialization procedure automatically marks the HOME variables for environment variables so that other commands and shell scripts can use it. In Customizing the tcsh shell, you saw that in a typical .tcshrc file for an individual user, the PATH variable is an environmental variable. Making the PATH variable an environmental variable ensures that search rules and changes to search rules are automatically shared by all shell sessions and scripts.