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?
- [Email protected] desktop]$ echo $HOME
- /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?
- [email protected] desktop]$ cat ~/.BASHRC
The contents are:
[Plain]View Plaincopy print?
- #. BASHRC
- # Source Global Definitions
- if [-F/ETC/BASHRC]; Then
- . /etc/bashrc
- Fi
- # 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?
- [Email protected] desktop]$ echo $HOME
- /home/taoge
- [Email protected] desktop]$ unset HOME
- [Email protected] desktop]$ echo $HOME
- [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?
- [Email protected] desktop]$ echo $HOME
- /home/taoge
- [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?
- #. BASHRC
- # Source Global Definitions
- if [-F/ETC/BASHRC]; Then
- . /etc/bashrc
- Fi
- # User specific aliases and functions
- # define permanent variable by Taoge
- 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?
- [Email protected] desktop]$ echo $winner
- [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?
- [Email protected] desktop]$ echo $winner
- People who persists
- [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?
- [[EMAIL PROTECTED] DESKTOP]$ ECHO $$  
- 7203
- [[email protected] desktop]$ echo $winner
- people who persists
- [[email protected] desktop]$ bash
- [[email protected] desktop]$ echo $$
-
- [[email protected] desktop]$ echo $winner
- PEOPLE WHO PERSISTS  
- [[Email protected] desktop]$ exit
- EXIT  
- [[Email protected] desktop]$ echo $$
- 7203  
- [[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?
- [[email protected] desktop]$ export x= "Defined in shell"
- [[email protected] desktop]$ vim a.sh
- [[EMAIL PROTECTED] DESKTOP]$ CAT A.SH   
- #! /bin/bash
- echo $x
- [[email protected] desktop]$ chmod +x a.sh
- [[EMAIL PROTECTED] DESKTOP]$ ./A.SH   
- defined in shell
- [[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?
- [Email protected] desktop]$ echo $x
- [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?
- [Email protected] desktop]$ z= "f (y)"
- [Email protected] desktop]$ echo $z
- F (Y)
- [Email protected] desktop]$ echo $$
- 7578
- [Email protected] desktop]$ bash
- [Email protected] desktop]$ echo $$
- 7653
- [Email protected] desktop]$ echo $z
- [[Email protected] desktop]$ exit
- Exit
- [Email protected] desktop]$ echo $$
- 7578
- [Email protected] desktop]$ unset Z
- [Email protected] desktop]$ echo $z
- [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?
- [Email protected] desktop]$ z= "f (y)"
- [Email protected] desktop]$ echo $z
- F (Y)
- [Email protected] desktop]$ echo $$
- 7578
- [Email protected] desktop]$ Export Z
- [Email protected] desktop]$ bash
- [Email protected] desktop]$ echo $$
- 7723
- [Email protected] desktop]$ echo $z
- F (Y)
- [[Email protected] desktop]$ exit
- Exit
- [Email protected] desktop]$ echo $$
- 7578
- [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