Bash Parameters and Variables: Difference between revisions
Line 6: | Line 6: | ||
=Global Variable= | =Global Variable= | ||
Global variables are only maintained within the context of the current shell. Once the shell exits, the global variables are discarded. | |||
Global variables are ''not'' available to sub-shells, | |||
==<tt>export</tt>== | |||
<pre> | |||
export VAR=VALUE | |||
</pre> | |||
Declaring a global variable to be exported causes the variable and its value ''to be copied'' in the environment of a invoked sub-shell. The sub-shell gets a copy of the variable, not a reference. Modifying the variable from the sub-shell does not reflect into the value of the global variable maintained by the invoking shell. | |||
=<tt>local</tt> Keyword= | =<tt>local</tt> Keyword= |
Revision as of 00:54, 27 May 2016
Internal
Global Variable
Global variables are only maintained within the context of the current shell. Once the shell exits, the global variables are discarded.
Global variables are not available to sub-shells,
export
export VAR=VALUE
Declaring a global variable to be exported causes the variable and its value to be copied in the environment of a invoked sub-shell. The sub-shell gets a copy of the variable, not a reference. Modifying the variable from the sub-shell does not reflect into the value of the global variable maintained by the invoking shell.
local Keyword
The local keyword designates a local variable. Local variables can only be declared inside a function.
export
Scope
All variables declared in a shell are visible inside the functions executed within that shell and inside functions invoked from function invoked from the shell, recursively.