Home >Operation and Maintenance >Linux Operation and Maintenance >How to correctly set the PATH environment variable in Linux

How to correctly set the PATH environment variable in Linux

WBOY
WBOYOriginal
2024-02-22 20:57:03874browse

How to correctly set the PATH environment variable in Linux

How to correctly set the PATH environment variable in Linux

In the Linux operating system, environment variables are one of the important mechanisms used to store system-level configuration information. Among them, the PATH environment variable is used to specify the directories in which the system searches for executable files. Correctly setting the PATH environment variable is a key step to ensure the normal operation of the system. This article will introduce how to correctly set the PATH environment variable in Linux and provide specific code examples.

1. View the current PATH environment variable

Enter the following command in the terminal to view the PATH environment variable in the current system:

echo $PATH

With this command, we can see The current PATH environment variable of the system consists of multiple directory paths, which are separated by colons (:).

2. Modify the PATH environment variable

Temporarily modify the PATH environment variable

If you only need to modify the PATH environment variable temporarily, you can directly enter the following command in the terminal:

export PATH=/new/path:$PATH

This command will add /new/path to the front of the PATH environment variable. Please note that this modification method is only effective in the current terminal window and will become invalid after restarting.

Permanently modify the PATH environment variable

If you want to permanently modify the PATH environment variable, you can edit the configuration file .bashrc or .profile, The specific operations are as follows:

  1. Open the ~/.bashrc or ~/.profile file:
vi ~/.bashrc
  1. Add the following code at the end of the file:
export PATH=/new/path:$PATH
  1. Save the file and exit the editor;
  2. Enter the following command to make the changes effective:
source ~/.bashrc

3. Verify whether the PATH environment variable is set correctly

In order to verify whether the PATH environment variable is set correctly, you can enter the following command:

which executable_filename

whereexecutable_filename is the one you are looking for Executable file name. If the terminal can find the executable file and output its path, the PATH environment variable is set correctly.

Conclusion

Correctly setting the PATH environment variable in Linux is crucial to the normal operation of the system. Through the method described in this article, you can easily modify and verify the PATH environment variable. I hope this information is helpful to you, and I wish you a happy use of your Linux system!

Related reading

  • [Detailed explanation of Linux environment variable configuration](https://linuxtools-rst.readthedocs.io/zh_CN/latest/base/03_linuxenv.html)
  • [Linux environment variable configuration modification and effective method](https://cloud.tencent.com/developer/article/1781962)

Reference materials

  • [Setting and using Linux environment variables](https://www.ibm.com/support/knowledgecenter/ssw_aix_72/com.ibm.aix.osdevice/env_variables.htm)

Write here , we have learned how to correctly set the PATH environment variable in Linux and provided corresponding code examples. I hope this article is helpful to you, thank you for reading!

The above is the detailed content of How to correctly set the PATH environment variable in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn