Scope of "Linux" shell three variables--linux Shell "Permanent environment variable", "Temporary environment variable" and "normal variable" full interpretation

Source: Internet
Author: User
Tags aliases

2015-05-08 00:15 3896 people read review (US) Collection report This article has been included in:Classification:Advanced Software Development (419) Author of similar articles X
    unix/linux Miscellaneous (118) Author of similar articles X

      Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

      In this era, we write some books, but also just to make some money, basically is a list of some knowledge points, is basically written to the people who have already seen. There are not many people who really write books by heart. If you really write a book and stand on the reader's point of view, it's less.

      About the Linux shell environment variable, I read four Linux related book introduction, the result, not a book of the explanation let me completely satisfied, total feeling not to say clearly. So, I'm going to write it myself, if it's inappropriate, please correct me.

      A. Persistent environment variables (which are actually files, not shell, are loaded/imported into the shell for each shell that is opened, creating a temporary environment variable for the current shell)

      First say "permanent environment variable", in fact, I also know that nothing is really permanent, here is permanent refers to: variables stored in the file, not because of power loss or shutdown and disappear. Below, we open a Linux shell and print the value of home, as follows:

      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $HOME
        2. /home/taoge

      We see that the value of this variable is/home/taoge, where does the value of this variable come from? We can look at the. bashrc file in the user's home directory

      [Plain]View Plaincopy print?
        1. [email protected] desktop]$ cat ~/.BASHRC

      The contents are:

      [Plain]View Plaincopy print?
        1. #. BASHRC
        2. # Source Global Definitions
        3. if [-F/ETC/BASHRC]; Then
        4. . /etc/bashrc
        5. Fi
        6. # User specific aliases and functions
      #. bashrc# Source Global definitionsif [-F/ETC/BASHRC]; Then        ./etc/bashrcfi# User specific aliases and functions

      Huh? No home? Do not worry, first of all think is in the/ETC/BASHRC in the home set it, in this, we do not delve into, only need to have this understanding: Home and file ~/.BASHRC closely related, even if power off or power down, also not afraid to disappear.

      In fact, when we open a shell process, home This permanent environment variable is automatically imported into the current shell (for the current shell set a temporary environment variable), then this home can be unset off it? Let's take a look:

      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $HOME
        2. /home/taoge
        3. [Email protected] desktop]$ unset HOME
        4. [Email protected] desktop]$ echo $HOME
        5. [Email protected] desktop]$

      We see that the current home in the shell process is really unset off, don't worry, we open another shell process, and then see if there is a home, as follows:

      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $HOME
        2. /home/taoge
        3. [Email protected] desktop]$
      [Email protected] desktop]$ echo $HOME/home/taoge[[email protected] desktop]$
      As you can see, the second shell process has home, this is not difficult to understand, because when the second shell process, will be the ~/.BASHRC in the permanent home load once, so you can see/home/taoge.

      Let's take a moment to summarize: the persistent environment variable exists in the ~/.BASHRC file (after power-down or reboot, does not disappear), and when each shell starts, it imports the persistent environment variable into the shell and becomes the shell's temporary environment variable. This temporary environment variable can be unset off, but will not affect other shells, as we will soon say that the temporary environment variables of different shells are independent of each other.

      You may still be in a tangle of questions: Don't see home in ~/.BASHRC, aren't you talking nonsense? All right, let's do it ourselves. Writes a variable to the ~/.BASHRC file, making it a permanent environment variable, and the ~/.BASHRC file contains the following:

      [Plain]View Plaincopy print?
        1. #. BASHRC
        2. # Source Global Definitions
        3. if [-F/ETC/BASHRC]; Then
        4. . /etc/bashrc
        5. Fi
        6. # User specific aliases and functions
        7. # define permanent variable by Taoge
        8. Winner= "people who persists"
      #. bashrc# Source Global definitionsif [-F/ETC/BASHRC]; Then        ./etc/bashrcfi# User specific aliases and functions# define permanent variable by taogewinner= "people who Persis Ts
      I define winner The value of this variable is "people who persists", OK, save the file, let's look at winner this variable, as follows:
      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $winner
        2. [Email protected] desktop]$
      [Email protected] desktop]$ echo $winner [[email protected] desktop]$  

      Unfortunately, we did not see winner, why? Because now it's just turning winner into a permanent environment variable, this permanent environment variable is not loaded into the current shell! OK, let's turn off the current shell and open a new shell and look at it again, as follows:

      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $winner
        2. People who persists
        3. [Email protected] desktop]$

      Can see, this time winner have value, excited bar. In this way, regardless of whether to restart Linux later, or how to drop, winner becomes a part of the file, it becomes a permanent environment variable. Of course, if you remove the winner line from the ~/.BASHRC file, and then say to me, "Don't you say permanent?"  Now why not forever? Well, you're trying to pick a fault.

      two. Temporary environment variables (belonging to the current shell and its child processes)

      As we've already said, winner becomes a permanent environment variable, and when a shell is opened, it loads the winner variable, and in the current shell environment, the winner becomes a temporary environment variable. The reason is temporary, because you can unset him off, the reason is that the environment variable, meaning (when not unset off), the current shell process of the child process can access to the winner, as follows:

      [Plain]View Plaincopy print?
        1. [[EMAIL PROTECTED] DESKTOP]$ ECHO $$  
        2. 7203   
        3. [[email protected] desktop]$ echo  $winner   
        4. people who persists  
        5. [[email protected] desktop]$ bash   
        6. [[email protected] desktop]$ echo $$  
        7. [[email protected] desktop]$ echo  $winner   
        8. PEOPLE WHO PERSISTS  
        9. [[Email protected] desktop]$ exit   
        10. EXIT  
        11. [[Email protected] desktop]$ echo  $$  
        12. 7203  
        13. [[email protected] desktop]$    

      We see that the current process PID is 7203, for it to open a child shell process, the child process PID is 7354, we can see, in the process, can also access to winner.

      The above winner is a permanent environment variable loaded in ~/.BASHRC, can we customize the TEMP environment variable? OK. This time, we run the a.sh script to do the child process of the current shell, as follows:

      [Plain]View Plaincopy print?
        1. [[email protected] desktop]$ export x= "Defined in shell"   
        2. [[email protected] desktop]$ vim a.sh  
        3. [[EMAIL PROTECTED] DESKTOP]$ CAT A.SH   
        4. #! /bin/bash  
        5. echo  $x   
        6. [[email protected] desktop]$ chmod +x a.sh   
        7. [[EMAIL PROTECTED] DESKTOP]$ ./A.SH   
        8. defined in shell  
        9. [[EMAIL PROTECTED] DESKTOP]$   
      As you can see, in the script process, you can also access the X temporary environment variable.  OK, let me ask a question, what other shell can access this x? Let's start another shell, as follows:
      [Plain]View Plaincopy print?
        1. [Email protected] desktop]$ echo $x
        2. [Email protected] desktop]$
      [Email protected] desktop]$ echo $x [[email protected] desktop]$  

      Certainly not, some of the examples above reveal the nature of temporary environment variables: The current shell's temporary environment variables can be accessed by themselves and their child processes (child shell processes, sub-script processes, or sub-c program processes), but not by other shells (independent of each other). Yes, we have discussed above, temporary environment variables can be unset off. In the actual large-scale software development, the compilation of large projects, often need to use temporary environment variables.

      three. Normal variable (belongs to the current shell process)

      The normal variables in the shell are simple and can be accessed only by the current shell, not by their child processes, or by other shells. Of course, it can also be unset off, tested as follows:

      [Plain]View Plaincopy print?
      1. [Email protected] desktop]$ z= "f (y)"
      2. [Email protected] desktop]$ echo $z
      3. F (Y)
      4. [Email protected] desktop]$ echo $$
      5. 7578
      6. [Email protected] desktop]$ bash
      7. [Email protected] desktop]$ echo $$
      8. 7653
      9. [Email protected] desktop]$ echo $z
      10. [[Email protected] desktop]$ exit
      11. Exit
      12. [Email protected] desktop]$ echo $$
      13. 7578
      14. [Email protected] desktop]$ unset Z
      15. [Email protected] desktop]$ echo $z
      16. [Email protected] desktop]$
      [Email protected] desktop]$ z= "f (y)" [[email protected] desktop]$ echo $zf (y) [[email protected] desktop]$ echo $$7578[[em AIL protected] desktop]$ bash[[email protected] desktop]$ echo $$7653[[email protected]st desktop]$ echo $z [[Email Protect ED] desktop]$ Exitexit[[email protected] desktop]$ echo $$7578[[email protected] desktop]$ unset z[[email protected] Deskt op]$ echo $z [[email protected] desktop]$
      It can be seen that the shell does not have access, of course, it must not be the other shell access. Normal variables to improve the temporary environment variables, it is also very simple, add the export can be, as follows: [Plain]View Plaincopy print?
      1. [Email protected] desktop]$ z= "f (y)"
      2. [Email protected] desktop]$ echo $z
      3. F (Y)
      4. [Email protected] desktop]$ echo $$
      5. 7578
      6. [Email protected] desktop]$ Export Z
      7. [Email protected] desktop]$ bash
      8. [Email protected] desktop]$ echo $$
      9. 7723
      10. [Email protected] desktop]$ echo $z
      11. F (Y)
      12. [[Email protected] desktop]$ exit
      13. Exit
      14. [Email protected] desktop]$ echo $$
      15. 7578
      16. [Email protected] desktop]$
      To summarize: the normal variables in the shell can only be accessed by the current shell, not by their child processes, and not by other shells. Of course, it can also be unset off.

      OK, I think I should be clear, early to rest!

      Add:

      1. In fact, we can also set alias aliases in ~/.BASHRC, which is handy and can be used by each shell (including the child shell). Once modified, you don't have to turn off the shell to open the shell, just execute the source ~/.BASHRC in the current shell.

      2. If you define an alias in the current shell, then only valid in the current shell process, we cannot use export to make it take effect in the child shell, after all, alias and the above variables are different. If you define alias in a script, you also have to do it with source, so that alias takes effect in the current shell, which I often play.

      Resources:

      http://blog.csdn.net/stpeace/article/details/45567977

      Shell If Else statement: http://c.biancheng.net/cpp/view/7005.html

      Http://www.jb51.net/article/34332.htm

      Http://zhidao.baidu.com/link?url=Autj00HdRPo5jA_Y5739uGonHER4rfWXR_uGnpuEMwl829D5jmftWPBJ9fYUI0kvxqaurUEcH4VyMrz2Ts0rSK

      Scope of "Linux" shell three variables--linux Shell "Permanent environment variable", "Temporary environment variable" and "normal variable" full interpretation

      Related Article

      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.