Monday, March 20, 2017

Unix Command : EXPORT

export is a built-in command of the bash shell  It is used to mark a shell variable for export to child processes.

Example-
# .bash_profile
JAVA_HOME="/java/jdk"

export PATH JAVA_HOME
When you execute a program the child program inherits its environment variables from the parent. For instance if $HOME is set to /root in the parent then the child's $HOME variable is also set to /root.
This only applies to environment variable that are marked for export. If you set a variable at the command-line like
$ FOO="bar"
That variable will not be visible in child processes. Not unless you export it:
$ export FOO
You can combine these two statements into a single one in bash (but not in old-school sh):
$ export FOO="bar"
Here's a quick example showing the difference between exported and non-exported variables. To understand what's happening know that sh -c creates a child shell process which inherits the parent shell's environment.
$ FOO=bar
$ sh -c 'echo $FOO'

$ export FOO
$ sh -c 'echo $FOO'
bar

No comments:

Post a Comment