Understanding and manipulating environment variables (MAC-based operations)

Source: Internet
Author: User

Through this article, the simple understanding of environment variables and their operations, and easy to meet the relevant problems can be accurately and quickly resolved.

What is an environment variable

 is Dynamic
Running processes would behave on a computer. ---wikipedia

Turning into Chinese is probably "environment variable is a dynamically named value, this value can affect how the computer runs the program", followed by an example to understand may be better ~

When the user enters a command in the terminal and knocks the return key, the computer locates the file in the path specified in the environment variable and executes the file to achieve the required program for running the user, such as:

# View Environment Variables Echo $PATH # enter the specified path under cd/usr/local/bin# to See what node-related things "node"

Effect

Open the folder and find that node is an executable file that contains the contents of the node command:

When the user executes the node command at the terminal, through the environment variable, the system points the node command to the folder and executes the corresponding content in that folder to start the node program.

In order to verify, the node file name is changed here, the prediction when node renamed Node_test, the execution of the Node_test command is to execute the previous node program, the following is the validation result diagram:

# Modify the node file named Node_test mv Node Node_test # See what node-related things are, and find that no node has become a node_test " node " # Execute the node command to prompt for no such command node # execute node_test, found the same as the original node effect, and can write code, but also to see the version, all normal  -V

Effect

With such a simple example, you can roughly know what the environment variables are, and what the environment variables are for, and then look at how to operate and how to better manage the environment variables.

Configuring Environment variables

Configure using the Execute command line

# exposing a new variable New_pathexport new_path=/usr/lib#   We can see its value by $NEW _path variable name #  Append the New_path to the path variable, note: Split path with: symbol export path= $PATH: $NEW _path

Effect:

The result shows OK, the correct append to the $path ...

The temporary modification of the environment variable can also do so, but it is not recommended that such an operation rewrite all the contents of the path:

Eval path=xxx

Effect

Based on the above method to add an environment variable is temporary, re-open the terminal interface, everything is back to the same ~ then want to let the configuration of the environment variable is permanent how to deal with it? We can use the environment variables to be configured to write to the corresponding file, the system will execute the corresponding files at the specified time to read the user-configured commands.

Configuring environment variables with files

MAC OS Default environment variable configuration file path is /etc/paths , and this file is also the Global environment variable file, generally read-only permissions, need to use the Administrator account to modify permissions, and then write the corresponding environment variable value, operation as follows:

Open a new terminal interface, output $path, effect

With this modification, any user entering this computer can use the specified global environment variable (e.g./etc/path_test).

Most of the time, the above configuration will not be used to change the global environment variables, which are generally configured for the current logged on user environment variables, then you should operate ~/.bash_profile

As an example, add an environment variable to the ~/.bash_profile

# in. Bash_profile, enter exoprt test_path=/users/yeshouexport PATH= $PATH: $TEST _path

Effect

A small example of an environment variable operation under a Mac basically runs this way, and it also has a general understanding of the environment variables and their operating procedures.

Finally, let's take a look. What are the bash* related documents:

CD ~A | grep "Bash"

The listed files are:

. bash_history (file that records the Bash command operation)

. Bash_profile (environment variable configuration files that are executed when the system is logged on, such as booting the system, Telnet, switching users)

. Bash_sessions (Bash session record, this is a folder that contains a list of files for each action you make with bash)

. BASHRC (The system reads the environment variable profile that is executed during interactive and non-logon bash shell operations)


Normal operation is quite a slip, in the finishing of the article will encounter a lot of problems, which also reflects the usual stay in the use of the stage, did not stop to think to understand ... By writing articles to summarize and organize knowledge points, you will find that the daily understanding or operation of a lot of there is not so suitable place, found to correct, so that there will be progress ~

Understanding and manipulating environment variables (MAC-based operations)

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.