Exporting 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 like a separate shell session. By default, it does not share any variables with your current shell session. If you define a variable VAR in the current session, it is local to the current session; any shell script that you call will not know about VAR.
export VAR
The export command
says that you want the variable VAR passed
on to all the commands and shell scripts that you execute in this
session. After you do this, VAR becomes global and the
variable is known to all the commands and shell scripts that you use.MYNAME="Robin Hood"
export MYNAME
Now all your commands 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, Robin Hood
,
obtained from the MYNAME variable. When a script begins running, it automatically inherits all the variables currently being exported. However, if the script changes the value of one of those variables, that change is not reflected to the calling shell—unless you run the script with the dot ( .) utility.
export name
indicates
that the variable with the given name should
be exported. When other programs are run from that script, they inherit
the value of all exported variables. However, when the script ends,
all its exported variables are lost to the calling shell.Some variables are automatically marked for export by the software that creates them. For example, if you invoke the shell, the initialization procedure automatically marks the HOME variables for export so that other commands and shell scripts can use it. In Customizing the z/OS shell, you saw that in a typical .profile file for an individual user, the PATH variable is exported. Exporting PATH ensures that search rules and changes to search rules are automatically shared by all shell sessions and scripts.
You must export other variables explicitly, using the export command.