Earlier I mentioned that the shell variables are not visible in child processes whereas environment variables are. We can export shell variables so that they are available in the child processes. The export command is used for this purpose. In our previous example of setting the NAME variable, we can export it to make it visible for other processes that our shell creates.
In the next example, I demonstrate the difference between exported and nonexported variables. First we create a variable NAME and then start a child shell by executing the sh command. When we use the echo command in the child shell for the NAME variable, it is empty. Now we use the exit command to return to the parent shell and export the NAME variable. We start the child shell again, and now the variable is visible in the child shell. Finally, we use exit again to return to the parent shell.
$ NAME="Mike Ron" $ echo $NAME Mike Ron $ sh $ echo $NAME $ $ exit $ export NAME $ sh $ echo $NAME Mike Ron $ exit $
Note
If you make a change to an environment variable, always export it. This is necessary so that the correct value of the variable is inherited by the child processes.
Top |