Linux environment variable Grooming

Source: Internet
Author: User

There are two kinds of environment variables in Linux: Global variables and local variables;

Global variables are shared by all processes, regardless of how many shell sub-processes are created (or how many command-line window windows are open), variables that they can access can be called global variables.

Local variables are scoped only to the current shell process and the child processes of the current shell process, and other cases cannot access the process's local variables.

Define, access, delete local variables

  

View global Variables

You can use the PRINTENV or ENV command to print all global variables.

  

To access a global variable, you can use Printenv key, or Echo $key

  

These global variables, whether in the command-line interface (in fact, the current process), or in the Open command-line interface, can be accessed using the same command, and the values are the same, and such variables are called global variables.

Setting Global variables

  

  Note After you set the global variable, you use export to import it into a global variable, otherwise it is considered a local variable.

  At this point, if you open another command-line window, another process, you are not accessing the defined global variable Myglobalvar. The global variable is destroyed after the process that defines the variable is finished.

To allow the global variables you define to be shared by all command-line windows, you can write commands that declare global variables inside $HOME/.BASHRC, and then execute them. The. bashrc or source. BASHRC command lets the script perform a bit.

  For example: Note the last line

  

As for why write in this file, you can self-Baidu shell Terminal load configuration file order.

Watch out.

1, whether it is the definition of local variables or global variables, the format is the same, key=value, there is no space on both sides of the equal sign

2. The echo $key can be used to access both local and global variables.

3, global variables and local variables can be deleted using unset, it should be noted that the variable name does not add $.

4, only when accessing the value of the variable, before the variable name plus $, other cases, such as assignment, delete, export are not added $. There is one exception, printenv access global variables do not $.

5, global variables more than local variables an export command, do not export, is always a local variable.

6. If the global variable is not written in the. BASHRC, then the life cycle is limited to the end of the current process.

Easy to point (the scope of the variable)

The scope of the local variable is limited to the current process. The child process of the current process cannot access local variables defined in the parent process.

  

Global variables are scoped to the current process, as well as the descendant processes of the current process (child processes, child processes of child processes ...). )

  

  

After a global variable is persisted (declared in. bashrc), any process can access it, and any child processes of any process can also be accessed.

  

Linux environment variable Grooming

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.