Shell environment and variable survival learned from the export command
Source: Internet
Author: User
Article title: The Shell environment and variable lifetime learned from the export command. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
When executing a script, a sub-shell environment is started first (I don't know if this is the case for executing other programs), and then all the system environment variables in the parent shell are copied, the statements in this script are executed in the sub-shell. (That is, the environment variable of the parent shell can be called in the sub-shell, but in turn it won't work. if the environment variable is defined in the sub-shell, it is only valid for this 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'
After executing the command with source, you can see this variable in shell, but when executing bash to open a sub-shell, test will not be copied to the sub-shell, because the execution script file is actually run in a sub-shell, when I create another script file for execution, nothing will be input, 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?
The conclusion is as follows: 1. when a script is executed, it runs in a sub-shell environment. after the script is executed, the sub-shell automatically exits; 2. 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 only valid for the shell or its sub-shell. when the shell ends, the variables disappear (and cannot be returned 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: Why is one script executed directly and another line executed with source? This is also a problem I encountered. Manual: Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. do you understand why? Directly executing a script file runs in a sub-shell, while source runs in the current shell environment.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service