Three Methods of Setting PATH Environment Variable of in Linux System

  • 2021-07-10 21:12:58
  • OfStack

1. Under Windows system, many software installations need to configure environment variables, such as installing jdk. If you do not configure environment variables and run javac command in a directory other than software installation, you will report that you can't find files and similar errors.

2. So what are environment variables? To put it simply, it is to specify a directory. When running the software, the related programs will look for related files according to this directory. The most practical function of setting variables is that you don't have to copy some dll files to the system directory, and path is the system search dll file series path

Under the Linux system, if you download and install an application, it is very likely that the prompt "command not found" will appear when you type its name. If you go to the installation directory folder every time, it is too cumbersome to find the executable file for operation. This involves the setting of environment variable path, and the setting of Path is also a component of customizing environment variable under Linux

Three methods of setting environment variables under Linux:

To add a path to $PATH, do this:

Environment variables that only work on the current shell

1. Setting in the console is not in favor of this method, because it only works for the current shell, and changing one shell setting will be invalid:

Enter: $PATH= "$PATH":/NEW_PATH in the direct console (turning off shell Path reverts to the original path)

Environment variables that work for all users

2. Modify the/etc/profile file. If your computer is only used as push for development, because all users' shell have the right to use this environment variable, it may bring security problems to the system. This is for all users, all shell


vi /etc/profile

At the bottom of /etc/profile, add: export PATH = "$PATH:/NEW_PATH"

Environment variables that work for a specific user at present

3. Modify bashrc file. This method is safer. It can control the permission to use these environment variables to the user level. This is for a specific user. If you need to give a user permission to use these environment variables, you only need to modify the. bashrc file under his personal user's home directory.


vi ~/.bashrc

Add below:


Export PATH="$PATH:/NEW_PATH"

Ubuntu Linux System Environment Variable Configuration File:

/etc/profile: The first file used by the operating system to customize the user's environment when logging in. This file sets the environment information for each user of the system and is executed when the user logs in for the first time.

/etc/environment: The second file used by the operating system when logging in. Before reading your own profile, the system sets the environment variables of the environment file.

~/. profile: The third file used when logging in is the. profile file. Each user can use this file to enter shell information for their own use. When the user logs in, this file only executes once! By default, he sets 1 environment variable and executes the user's. bashrc file.

/etc/bashrc: Executes this file for every user running bash shell. When bash shell is opened, the file is read.

~/. bashrc: This file contains bash information specific to your bash shell, which is read when logging in and every time a new shell is opened.

PASH Environment Variable Setting Method:

Method 1: The. profile or. bashrc file in the user's home directory (recommended)

Log in to your user (non-root) and enter:


$ sudo gedit ~/.profile(or .bashrc) 

You can add the following settings to PATH at the end of this file:


export PATH= " $PATH:your path1:your path2 ... "  

Save the file, log out and log in again, and the variable takes effect.

Variables added in this way are only valid for the current user.

Method 2: profile file in the system directory (caution)

In the etc directory of the system, there is 1 profile file, edit this file:


$ sudo gedit /etc/profile 

The settings for adding PATH at the end are as follows:


export PATH= " $PATH:your path1:your path2 ... "  

After editing and saving the file, restart the system and the variable will take effect.

Variables added in this way are valid for all users.

Method 3: environment file in the system directory (caution)

In the etc directory of the system, there is 1 environment file, edit this file:


$ sudo gedit /etc/environment 

Find the following PATH variable:


PATH="<......>" 

Modify the PATH variable and add your own path, for example:


PATH="<......>:your path1:your path2  … " 

Each path is divided by a colon. The file is also restarted to take effect, affecting all users.

Note that export PATH = is not added here.

Method 4: Enter directly under the terminal


vi ~/.bashrc
0

In this way, the variable takes effect immediately, but the setting becomes invalid after the user logs out or the system restarts, which is suitable for the setting of temporary variables.

Note: Methods 2 and 3 need to be modified with caution, especially through root users. If the modification is wrong, it may lead to some serious system errors. Therefore, the author recommends the first method. In addition, the development of embedded Linux should not be carried out under root (unless you are already very familiar with Linux!!) To avoid serious system errors caused by improper operation.

The following is an example of a problem caused by an incorrect modification of the environment file and its solution:

Problem: Unable to log in due to careless setting of environment variables in etc/environment

Tip: Do not set export PATH in etc/environment. This will cause you to be unable to log in to the system after restarting

Solution:

Enter the command mode in the login interface alt + ctrl+f1. If it is not root, the user needs to type (root, the user is not allowed to be so wordy, and gedit editing will not be displayed)
/usr/bin/sudo /usr/bin/vi /etc/environment

Move the cursor to line export PATH** and press d twice in succession to delete the line;

Input: wq save exit;

Then type/sbin/reboot to restart the system (need to boot may be prompted, in this case power off directly)

By Joan
Source: http://www.cnblogs.com/Joans/


Related articles: