Home > Hosting > Server

How to Modify Environment Variables in Linux Operating System

2023-03-17 14:50:01

<p style="text-align: left;">This article mainly explains how to modify environment variables in the Linux operating system. Interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn how to modify environment variables in the Linux operating system!</p><p style="text-align: left;"><strong>Method 1: Add variables to the /etc/profile file [effective for all users (permanent) 】</strong></p><p style="text-align: left;">Use vi to add variables in the file /etc/profile, this variable will be valid for all users under linux, and it is permanent.</p><p style="text-align: left;">To make the modification take effect immediately, you need to execute the following code</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;"># source /etc/profile<br/></p><p style="text-align: left;"><strong>Method 2: Add variables in the .bash_profile file in the user directory [effective for a single user (permanent)]</strong></p><p style="text-align: left;">Use vi in the user directory Variables are added to the .bash_profile file, and the changes are only valid for the current user and are permanent.</p><p style="text-align: left;">To make the modification take effect immediately, you need to execute the following code in the user directory</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;"># source .bash_profile<br/> </p><p style="text-align: left;"><strong>Method 3: Directly run the export command to define variables [only valid for the current shell (bash) (temporary)]</strong></p><p style="text-align: left;">Under the command line of the shell Directly use [export variable name=variable value] to define a variable. This variable is only valid under the current shell (bash) or its subshell (bash). When the shell is closed, the variable will be invalid. When opening a new shell There is no such variable, and it needs to be redefined if it needs to be used.</p><p style="text-align: left;"><strong>Method 4: Direct assignment</strong></p><p style="text-align: left;">Input in the command line</p><p style="text-align: left;">Copy code The code is as follows:</p><p style="text-align: left;">path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin<br/></p><p style="text-align: left;">Using this method is only valid for the current session, and also That is to say, whenever you log out or log out of the system, the path setting will become invalid.</p><p style="text-align: left;"><strong>Method 5: Modify the /ect/profile file</strong></p><p style="text-align: left;">Add at the end of the file</p><p style="text-align: left;">Copy the code The code is as follows:</p><p style="text-align: left;">Export path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin<br/></p><p style="text-align: left;">//Note: The two sides of the &quot;=&quot; number cannot This method is the best if there are any spaces.</p><p style="text-align: left;">Unless you manually force the value of path to be changed, it will not be changed. <br/></p><p style="text-align: left;"><strong>Method 6: Modify the .bachrc/.bash_profile file</strong></p><p style="text-align: left;">Add at the end of the file</p><p style="text-align: left;">Copy the code The code is as follows :</p><p style="text-align: left;">export path=$path:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin<br/></p><p style="text-align: left;">This method It works for the current user, and it will also fail when you log out of the system.</p><p style="text-align: left;">Note: For methods 2 and 3, if you want the path to take effect, you must log in again to achieve it. The following methods can simplify the work: If you modify After editing /etc/profile, execute the source profile or execute the dot command. The value of the profile path will take effect immediately. The principle of this method is to execute the /etc/profile shell script again. Note that if you use sh /etc/profile, it will not work, because sh is executed in the subshell process, even if the path changes, it will not be reflected in the current environment. , but source is executed in the current shell process, so we can see the change of path.</p><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us