Creation: 00:49:46
Author: unlinux
From: http://www.Unlinux.com
Me
I am a newbie myself. I haven't been in touch with Linux for a long time. Recently I encountered a problem when I learned the bash Export command (the book says that export is changing the User-Defined variable to the system environment ).
Volume): I defined a variable in a script file, and then the export variable, according to my own ideas, after executing this script, at the prompt, you can use echo to display its value.
This is not the case. After the script is executed, the set variable does not exist at all. Why? I was puzzled and finally posted the problem. A senior told me to use the source + script file.
I tried it, but a new problem came out. After the Export command in the script is deleted, it can be used as source. This export seems useless.
Yes.
After several attempts, I found something that I guessed. If anything is wrong, please correct me. Thank you.
When executing a script, a sub-
Shell environment (do not know if other programs are like this), and then copy all the system environment variables in the parent shell, the statements in this script will be executed in the Child shell. (That is
The environment variables of the parent shell can be called in the sub-shell, but in turn cannot be called. If the environment variables are defined in the sub-shell, they are only valid for the shell or its sub-shell,
When the sub-shell ends, the variable disappears after the script is executed .) To prove this, see the script content:
Test = 'value'
Export Test
After such a script is executed, test does not actually exist. Next, let's look at the following:
Test = 'value'
Export Test
Bash
Here we open another sub-shell in the last line of the script, which should be the sub-shell of the shell where the script file is located. After the script is executed, we can see the variable test, because it is in its sub-shell, when exit is used to exit the sub-shell, the test variable disappears.
If you use source to execute the script without export, you will not see this variable in the sub-shell, because it is not a system environment variable. For example, the script content is:
Test = 'value'
Use Source
After the script is executed, you can see this variable in shell, but when you execute Bash to open a sub-shell, test will not be copied to the sub-shell, because the execution script file is also in
A sub-shell is run, so when I create another script file for execution, nothing will be input, content such as: Echo
$ Test. So pay special attention to this. We can use echo $ test to output the variable value at the prompt. Why can't we put it into the script file?
So the conclusion is as follows:
Yes: 1. The script is run in a sub-shell environment. After the script is executed, the sub-shell automatically exits. 2. The system environment variables in a shell will be copied to the sub-shell.
(Variables defined using export); 3. The system environment variables in a shell are valid only for the shell or its sub-shell. When the shell ends, the variables disappear (and cannot be returned ).
Return to the parent shell ). 3. Variables not defined by export are only valid for this shell and are also invalid for the sub-shell.
Later, according to the prompts of the Moderator, I sorted out the post
Sub: Why is the direct execution of a script different from that of the source script? This is also a problem I encountered. The original manual is like this: read and execute
Commands from filename in the Current Shell environment and return
Exit status of the Last Command executed from
Filename. Understand why is it different? Directly executing a script file runs in a sub-shell, while source runs in the Current Shell environment. According
You have already understood the above.
The problems that have plagued me for a few days can finally be solved successfully.
Reprinted from: http://www.unlinux.com/doc/shell/20051026/251.html