Conclusion:
By default, the variables for the parent shell and child shell are isolated.
SH mode to run the script, it will re-open a child shell, unable to inherit the normal variables of the parent process, can inherit the parent process export global variables.
Source or. The script is run under the current shell, which is equivalent to loading the contents of the script into the current shell and then using the previously defined variables.
Validation: Calling the parent shell normal variable in a child shell
[Email protected] scripts]#Echo$b [[email protected] scripts]#Echo$a [[email protected] scripts]# b=Gaga[[email protected] scripts]#Echo$bgaga [[email protected] scripts]#CatTest1.SHa=hahaEcho "test1: $a"Echo "test1: $b"SH/root/scripts/test2.SH[email protected] scripts]#CatTest2.SH Echo "test2: $a"Echo "test2: $b"[email protected] scripts]#SHTest1.SHtest1:hahatest1:test2:test2: #执行过程解释:SHTest1.SH==>Restart a child Shella=haha ==>A variable assignmentEcho "test1: $a"==>Output: Test1:hahaEcho "test1: $b"==>output: test1: Because the child shell does not inherit the normal variable of the parent shell, $b is emptySH/root/scripts/test2.SH==>Restart a child shellEcho "test2: $a"==>output: test2: Ditto, $a is emptyEcho "test2: $b"==>output: test2: Ibid., $b is empty [[email protected] scripts]# source test1.SHTest1:hahatest1:gagatest2:test2:[[email protected] scripts]#Echo$ahaha # Execution procedure explained: source Test1.SH==>Execute script A under the current shell=haha ==>A variable assignmentEcho "test1: $a"==>Output: Test1:hahaEcho "test1: $b"==>output: Test1:gaga defines the b variable at the terminal before running the script. SH/root/scripts/test2.SH==>Restart a child shellEcho "test2: $a"==>output: test2: $a undefinedEcho "test2: $b"==>output: test2: $b undefined [[email protected] scripts]#Echo$a ==> output: Haha,source test1.sh is defined.
validation: Calling the parent shell normal variable in a child shell
Validation: Calling the export global variable defined by the parent shell in a child shell
[Email protected] scripts]#Echo$b [[email protected] scripts]#Echo$a [[email protected] scripts]#CatTest1.SHExport a=hahaEcho "test1: $a"Echo "test1: $b"SH/root/scripts/test2.SH[email protected] scripts]#CatTest2.SH Echo "test2: $a"Echo "test2: $b"[email protected] scripts]# Export B=Gaga[[email protected] scripts]#SHTest1.SHtest1:hahatest1:gagatest2:hahatest2:gaga# Output Description, the parent shell defines a global variable that can be passed to the child shell and the child Shell's child shell
validation: Calling the export global variable defined by the parent shell in a child shell
[Email protected] scripts]#Echo$b [[email protected] scripts]#Echo$a [[email protected] scripts]#CatTest1.SHExport a=hahaEcho "test1: $a"Echo "test1: $b"SH/root/scripts/test2.SH[email protected] scripts]#CatTest2.SH Echo "test2: $a"Echo "test2: $b"[email protected] scripts]# Export B=Gaga[[email protected] scripts]#SHTest1.SHTest1:hahatest1:gagatest2:hahatest2:gaga[[email protected] scripts]#Echo$a [[email protected] scripts]# #最后的 $a output is empty, stating that the defined global variables and normal variables are automatically destroyed when the child shell finishes running.
Validation: The export global variable defined by the child shell cannot be called in the parent shell
Note: If you use source to run the script during testing, exit the terminal and perform additional tests to prevent the value of the variable from affecting other validations.
Summary of inheritance of shell variables