Linux PATH variable notes

Table of contents

1. First, the path in the PATH variable is separated by colon:

2. What is the difference between export PATH="/app/mycat/bin:$PATH" and export PATH="$PATH:/app/mycat/bin"?

3. Modify the PATH variable value


1. First, the path in the PATH variable is separated by colon:

2. What is the difference between export PATH="/app/mycat/bin:$PATH" and export PATH="$PATH:/app/mycat/bin"?

Query commands in PATH are searched in the order of paths. For example, when searching for a command in PATH in the picture above, the search order is:

(1)/usr/local/sbin
(2)/usr/local/bin
(3)/usr/sbin:/usr/bin
(4)/root/bin

So the difference between them is that one command is placed at the front of the PATH variable, and the other is placed at the end. The difference is not very big. The path at the front will definitely take a shorter search time, but it can be ignored; in addition, if the command appears with the same name, and they are all in the path under the PATH variable, the one found first shall prevail.

3. Modify the PATH variable value

1. You can temporarily modify the PATH variable value by exporting PATH="/app/mycat/bin:$PATH". This modification is only at the session level and will only take effect in the current session;

2. Find the .bashrc file in the user's home directory, open it with a text editor, add a line export PATH="/app/mycat/bin:$PATH" at the end of the file, save and close the file. Then enter source ~/.bashrc in the command prompt and press Enter to make the changes take effect. This method is permanent for the current user and will be loaded automatically every time the shell is started.

vim /用户主目录/.bashrc


source /用户主目录/.bashrc

 PS: If we modify this file while the system is running, it will not take effect automatically, but will need to wait until the next time the system starts. If we want the modification to take effect immediately, we need to manually execute this file and let it reset the environment variables. source/user home directory/.bashrc or ./user home directory/.bashrc is the command to execute this file. Their function is to read and run the file/user home directory/.bashrc in the current shell environment, thereby making The modification takes effect.

3. Valid permanently for all users

The first way is to modify the /etc/profile file, add a line export PATH="/app/mycat/bin:$PATH", and source /etc/profile.d/mycat.sh or . /etc/profile.d/ mycat.sh;

The second method is to create a script file under /etc/profile.d/, write export PATH="/app/mycat/bin:$PATH" into the file, and then run this in the current shell environment. File or restart will also work.

echo "PATH=/app/mycat/bin:$PATH" >> /etc/profile.d/mycat.sh

source /etc/profile.d/mycat.sh 或 . /etc/profile.d/mycat.sh

Then the second approach is recommended. As for the reason, you can read the official explanation:

Guess you like

Origin blog.csdn.net/qq_54381110/article/details/131789670