Understanding Shell environment and variable lifetime from Export

Source: Internet
Author: User

I am also a newbie myself. I haven't been in touch with linux for a long time. I recently encountered a problem when I learned the BASH export command (the book says that export is used to change a custom variable to a system environment variable ): I defined a variable in a script file, and then the export variable. According to my own ideas, after executing this script, the echo can be used to display its value at the prompt, but this is not the case. After the script is executed, you cannot see this variable in set. Why? I was puzzled and finally posted the problem. A senior told me that I could use the source + script file. I tried it, but a new problem has emerged. After the export command in the script is deleted, it can be used as source. It seems that this export is useless.

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 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: after test = 'value' is executed with source, this variable can be seen in shell. However, when bash is executed 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. The script runs in a subshell environment. After the script is executed, the subshell 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. Based on the previous content, you have understood the truth. The problems that have plagued me for a few days can finally be solved successfully.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.