Methods to modify environment variables in the Linux operating system

  • 2020-05-14 05:32:13
  • OfStack

Method 1: add variables to the /etc/profile file [active (permanent) for all users]

Add a variable to the file /etc/profile file with VI. This variable will be valid for all users under Linux and will be "permanent".

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


# source /etc/profile

Method 2: add a variable to the.bash_profile file in the user directory.

Add a variable to the.bash_profile file in the user directory with VI. The change is only valid for the current user and is "permanent."

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


# source .bash_profile

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

Use [export variable name = variable value] directly from the command line of shell to define the variable. This variable is only valid under the current shell(BASH) or its child shell(BASH). When shell is closed, the variable is no longer valid.

Method 4: direct assignment

Type on the command line


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

With this approach, the PATH Settings are only valid for the current session, which means that each time you log out or log out of the system, the PATH Settings become invalid.

Method 5: modify the /ect/profile file

Add at the end of the file


export PATH=$PATH:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin

/ / note: there should be no Spaces on either side of the "=" sign

The value of PATH will not be changed unless you manually force it.

Method 6: modify the.bachrc /.bash_profile file

Add at the end of the file


export PATH=$PATH:/usr/lib64/ruby/gems/2.1.0/gems/jekyll-2.5.3/bin

This method works for the current user and will fail when you log out of the system

Note: for the 2,3 methods, you must log in again for PATH to take effect. The following method can simplify the work: if you modify /etc/profile, then the source profile or the command. The principle of this method is to execute the etc/profile shell script once more. Please note that sh /etc/profile is not ok, because sh is executed in the child shell process. Even if PATH changes, it will not reflect the current environment, but source is executed in the current shell process, so we can see the change of PATH.

The above article introduces you to modify the environment variables in the Linux operating system, I hope to help you.


Related articles: