Full interpretation of the Linux shell "permanent environment variables", "temporary environment variables" and "Normal variables"

Source: Internet
Author: User
Tags aliases

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:

[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

[email protected] desktop]$ cat ~/.BASHRC

The contents are:

#. 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:

[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:

[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.


Below, you may also be in a tangle of impatient questions: in the ~/.BASHRC did not see home ah, you are not 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:

#. 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:

[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:

[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:

[Email protected] desktop]$ echo $$
7203
[Email protected] desktop]$ echo $winner
People who persists
[Email protected] desktop]$ bash
[Email protected] desktop]$ echo $$
7354
[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:

[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:

[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:

[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]$

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:
[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!








Full interpretation of the Linux shell "permanent environment variables", "temporary environment variables" and "Normal variables"

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.