Accessing the parent process's user variables (local variables) in the child process, what should I do? ? ?
When we encounter such a demand, I have to say something about the export command.
Talk about the export command The
export command sets the user variable to an environment variable so that it can be accessed in the child shell process. This is exactly the same as the Chinese meaning of export. For the previous example, we can enter the command in the parent shell:
> export str When you execute the script again, you can output the value of the user variable str; this is the role of export. However, after we enter the export str in the parent shell, when we close the parent shell, the environment variable will be invalid. If you want to open the shell, you can set the export immediately. We can write the export str to the above summary according to our needs. That & ldquo; 坨 & rdquo; start the configuration file, so that will not cause the export to fail due to closing the parent shell.
Another relationship between parent and child
Now we can access the variable of the parent process in the child process. Have you ever thought about whether modifying the variable of the parent shell in the child shell will affect the variable in the parent shell? What is the value? May wish to do a test.
#!/bin/bash# Modify the value of the variable str passed by the parent shell $str='http://www.jellythink.com'echo $str Run the script and find the value of str in the parent shell and No change has taken place. In fact, this is related to another knowledge point about the process in Linux. When the parent process forks a child process, the child process will copy the relevant variables of the parent process. At this time, the child process will have the same value as the parent process. Although the same name has the same value, it is only a copy of the parent process. The modification to the copy is independent of the parent process. For information about Linux processes, you can refer to this article.
And if we define a variable in the child shell, is it accessible in the parent shell? Practice tells us that this doesn't work, you can't access the variables defined in the child shell in the parent shell. If you want to access the variables defined in the child shell in the parent shell, you can use a temporary file to write the local variables to the temporary file, and the parent shell reads the file to achieve the purpose of accessing the variables defined in the child shell.
Summary
Although this article is mixed and mixed, each part is closely related and interlocking. It is very helpful for everyone to understand Linux Shell as a whole, and it is also beneficial for everyone to learn Linux Shell. The article is a bit long and requires a little patience to read it from start to finish. In order to learn, it takes a little patience. What do you say? This one is summarized here, and the next one is goodbye.
Jelly Think - an original technical article sharing site.
October 21, 2015 in Hohhot.
Appendix
Here is a summary of the commands involved in the text and some related commands.
One of the benefits of learning Linux is that you can understand how the hardware works and the pri
under RedHat Enterprise Linux server 5.4 First, system environment operating system: RedHat Enterp
The reverse proxy can be divided into two types from the transmission: 1: synchronous mode (apache-
STM32F103VCT6 comes with two 12-bit DAC, DAC conversion speed has not been found, some people say th
Physical address to virtual address mapping
In linux, using reaver to crack the wireless wifi password download and use method
Yum error Error: Cannot find a valid baseurl for repo: addons
Analysis of the lower part of the Linux 2.6 interrupt
PXE+TFTP+DHCP network automatically boots and installs Linux
How to use sftp command under Linux
10 important Linux ps commands are actually
Use mutexes to control Linux thread synchronization
Add and remove programs via three-party software in Linux
Win8.1 input method is gone, what to do
Configuring Apache to disable the display of file lists
How to download movies from Baidu Video? Baidu video download movie method
When the Win8 system IE browser closes multiple web pages, what is the warning?
Microsoft released 13 security patches for the first time involving Windows7
Cleverly change the transfer mode to solve the FTP connection problem
How to set up a certain file type in the Windows 8 to open a file type
What is the Win10 flight mode?
Win7 patch KB3146706 blue screen solution
How does the Win10 system change the size of the text below the icon?