Get the App
SLTechnology News&Howtos  ›  Development  › 

How to modify environment variables in Linux operating system

Shulou Source: shulou.com Published: 2022-06-01 04:00:50 09月11日 Update

This article mainly introduces "how to modify the environment variable in the Linux operating system". In the daily operation, I believe that many people have doubts about how to modify the environment variable in the Linux operating system. The editor consulted all kinds of materials and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to modify the environment variable in the Linux operating system". Next, please follow the editor to study!

Method 1: add a variable to the / etc/profile file [effective for all users (permanent)]

Add a variable to the file / etc/profile file with vi, which will be valid for all users under linux and will be "permanent".

For the changes to take effect immediately, you need to execute the following code

The copy code is as follows:

# source / etc/profile

Method 2: add a variable to the. bash_profile file under the user directory [effective for a single user (permanent)]

Use vi to add variables to the. bash_profile file in the user directory, and the changes are only valid for the current user and are "permanent".

For the changes to take effect immediately, you need to execute the following code in the user directory

The copy code is as follows:

# source .bash _ profile

Method 3: run the export command directly to define variables [valid only for the current shell (bash) (temporary)]

Use [export variable name = variable value] to define a variable directly under the command line of shell. The variable is only valid under the current shell (bash) or its child shell (bash). When shell is closed, the variable is invalid, and there is no such variable when you open the new shell. If you need to use it, you need to redefine it.

Method 4: direct assignment

Enter on the command line

The copy code is as follows:

Path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin

Using this method, it is only valid for the current session, that is, every time you log out or log out of the system, the path setting becomes invalid.

Method 5: modify / ect/profile file

Add at the end of the file

The copy code is as follows:

Export path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin

/ / Note: there can be no spaces on both sides of the "=" sign. This is the best method except

Unless you manually force to change the value of path, otherwise it will not be changed.

Method 6: modify the .bachrc / .bash _ profile file

Add at the end of the file

The copy code is as follows:

Export path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin

This method works for current users, and it will fail when you log out of the system.

Note: for path 3 method, if you want to make path effective, you must log in again. The following method can simplify the work: if / etc/profile is modified, then the value of source profile or point command. / profile path will take effect immediately after editing. The principle of this method is to execute the / etc/profile shell script again, note that it is not possible to use sh / etc/profile, because the sh is executed in the child shell process, even if the path changes will not be reflected in the current environment, but the source is executed in the current shell process, so we can see the path change.

At this point, the study on "how to modify environment variables in the Linux operating system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Variables methods code files users systems environments valid operating systems commands permanent learning directories more ends processes now help no practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB Linux Apple NVidia