Linux PATH, PATH variable $PATH

Path problem under Linux

Environment configuration is a big difficulty for everyone to learn Linux, especially the various path configurations.

Today, let's talk about two or three things about PATH.

1. First, what is a path?

PATH, the PATH in the computer system is for files.

There are absolute paths and relative paths:

  • Absolute path, which must be written from the root directory / directory;
  • Relative path, not written by /;

2. PATH variable $PATH

Have you ever wondered why we can execute various commands in any directory of Linux?

This is due to the help of the environment variable PATH.

You can log in to your Linux system and enter echo $PATH. Note that the PATH here is capitalized.

[iron2222@localhost ~]$ echo $PATH

Note: each path will be separated by: and there are sequential differences between them.

When you enter a command, the system will find the corresponding executable file according to these paths, and use whoever finds it first.

3. How to modify path variables?

Especially when configuring the cross compilation tool chain, I was confused at that time:


For example, I newly downloaded a fun command that can generate a lot of interesting things, but I don't know why there is no response to the command input.

At this time, it is likely that your path is not configured properly.

For example, let's cancel the path of the most commonly used ls command and see what happens.

[iron2222@localhost ~]$ su - root
[root@localhost ~]# echo $PATH
[root@localhost ~]# mv /bin/ls /root
[root@localhost ~]# ls
-bash: ls: command not found

You will find that no matter which directory you are in, you can't execute the ls command.

Because the directory / root is not in the directory specified by PATH, it is impossible to find the command ls if you choose to be in the directory / root.

If you want root to execute ls in any directory, add / root to PATH.

[root@localhost ~]# PATH="${PATH}:/root"

You can check:

[root@localhost ~]# PATH="${PATH}:/root"
[root@localhost ~]# echo $PATH

Found that / root already exists.

[root@localhost ~]# echo $PATH
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  ls

At this time, the command ls can also be used.

After this experiment, don't forget to move the ls command back!!!

[root@localhost ~]# mv /root/ls /bin

At this time, you will suddenly find that why ls I have moved back, why can't I use it.

[root@localhost ~]# ls
-bash: /root/ls: No such file or directory
[root@localhost ~]# ls
-bash: /root/ls: No such file or directory

Don't be nervous. Just log out of exit and log in again.

[root@localhost ~]# exit
[iron2222@localhost ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[iron2222@localhost ~]$ su - root
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# 

That's it. Good morning, good afternoon and good night!

Tags: Linux Operation & Maintenance server

Posted on Thu, 02 Dec 2021 23:47:52 -0500 by fluvius