Linux advanced intelligence: variable $ PATH of the execution file PATH

Source: Internet
Author: User
Linux advanced intelligence: variable $ PATH of the execution file PATH -- Linux general technology-Linux technology and application information. For details, see the following. You should have noticed that: "Why can I execute the/bin/ls command anywhere ?" Why can I directly execute the ls command to display some information without saying that the/bin/ls command cannot be found? This is because of the help of the Environment Variable PATH. When executing a command, the system searches for the execution file in the PATH defined by each PATH according to the PATH settings. The first searched command is executed first. Now, enter the echo $ PATH command. echo means "display", while $ added before PATH indicates that the variable is followed, so the current PATH is displayed.

[Root @ linux ~] # Echo $ PATH/sbin:/usr/sbin:/bin:/usr/X11R6/bin:/usr/local/sbin

Have you noticed? /Bin is set in PATH, so you can find ls. PATH is a very important "variable" for execution files. It is mainly used to regulate the directory for command search. Each directory is ordered and separated by a colon ":" in the middle, as mentioned in the preceding example. What is the role of the PATH variable?

If you move ls to the/root directory (mv/bin/ls/root), you will also be in the/root directory (cd/root). However, when you execute ls, what should I do if it ignores you?

This is because the PATH variable does not have the/root directory, and you move the ls to the/root directory, the system will naturally not find the executable file, so it will tell you, command not found (command not found ). So how can we solve this problem? There are two methods. One is to directly add the/root PATH to the PATH.

How to add? You can use:
[Root @ linux ~] # PATH = "$ PATH":/root

In this way, add the PATH search directory. The other method is to use the complete file name to issue the command, that is, directly use the relative or absolute path to execute

For example: [root @ linux ~] #/Root/ls [root @ linux ~] #./Ls

Because in the same directory, and we know that the directory symbol in the same directory is ".", we can execute the above./ls. This execution method is common.

If there are two ls files in different directories, for example,/usr/local/bin/ls and/bin/ls. Which ls will be executed when ls is used? Which directory in PATH is queried first, then the files in which directory are executed first.

In this case, why not add the. directory in the PATH so that the file can be executed directly in the directory? Because. indicates the directory. Yes. However, have you ever wondered if a user writes an ls file in/tmp one day, but it is a harmful file? When you execute ls under/tmp, what should I do? It may be "won". Therefore, it is recommended that you do not add "." To the PATH search for security reasons.

You should understand the meaning of the sentence "why is the absolute path method much more correct than the relative path. Because the command is directly found for execution, rather than searching through the content of the PATH variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.