How to set the PATH environment variable in Linux system
In the Linux system, the PATH environment variable is used to specify the path where the system searches for executable files in the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples.
View the current PATH environment variable
Execute the following command in the terminal to view the current PATH environment variable:
echo $PATH
The above command will output the current system All paths are separated by colons.
Temporarily modify the PATH environment variable
If you only need to temporarily modify the PATH environment variable in the current session, you can use the export command. For example, to add /usr/local/bin to the current PATH environment variable, you can execute the following command:
export PATH=/usr/local/bin:$PATH
The above command adds the /usr/local/bin path to the front of the PATH environment variable. And keep the original path.
3.1 Modify the user’s personal PATH environment variable
If you want to keep the modified PATH environment variable every time you log in, Can be modified in the user's personal configuration file. The location and name of the configuration file will vary according to different Linux distributions.
For Bash Shell users, you can edit the .bashrc file in the user's home directory. Execute the following command to edit the file:
vi ~/.bashrc
Add the following line at the very bottom of the file to modify the PATH environment variable:
export PATH=/usr/local/bin:$PATH
Save the file and exit.
For users of other shells, you can edit the corresponding configuration file (such as .zshrc or .tcshrc) and modify it according to the above method.
3.2 Modify the system's PATH environment variable
If you want to modify the PATH environment variable in the entire system, you need to edit the system-level configuration file.
For Debian and Ubuntu systems, you can edit the /etc/environment file. Execute the following command to edit the file:
sudo vi /etc/environment
Add the following lines in the file to modify the PATH environment variable (note the colon separation):
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"
Save the file and exit.
For other Linux distributions, please find the corresponding system-level configuration file to modify.
For Bash Shell users, you can execute the following command to reload the modifications in the .bashrc file:
source ~/.bashrc
For other Shell or system-level configuration file modifications, please follow the corresponding way.
Verify the modified PATH environment variable
Execute the following command in the terminal to verify whether the PATH environment variable is successfully modified:
echo $PATH
The output should include the previous Added path.
Through the above steps, we can correctly set the PATH environment variable in the Linux system and verify the correctness of the setting through simple commands. Correctly setting the PATH environment variable can make our command line work more convenient and efficient.
The above is the detailed content of Steps to set the PATH environment variable of the Linux system. For more information, please follow other related articles on the PHP Chinese website!