z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Setting environment variables

z/OS UNIX System Services User's Guide
SA23-2279-00

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.

To deal with this situation, you can enter the following:
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.
As an example, suppose you enter the commands:
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.
Note: You could use single or double quotation marks to enclose the variable value. See Quoting variable values for more information.

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.

By default, any variables created within a shell script are local to that script. This means that when another program is run, those variables do not apply in its environment. However, the script can use the setenv command to turn shell variables into global environment ones. Inside a shell script:
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.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014