Variable about the execution PATH in Linux: $ PATH

Source: Internet
Author: User
For Linux, the variable $ PATH for the execution PATH is Linux universal technology-Linux programming and kernel information. For details, refer to the following section. After you mention the absolute path, relative path, and command issuing method, you should notice one thing, that is: "Why can I execute the/bin/ls command anywhere? "Yes! Why can I directly execute ls to display some messages without saying that the/bin/ls command cannot be found? This is because of the help of the Environment Variable PATH! When we execute a command, the system searches for the execution file in the PATH defined by each PATH according to the PATH setting. The command first found is executed first! Now, release echo $ PATH. echo indicates "display and print", 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? Yes! /Bin is in the PATH setting, so you can find ls! PATH is a very important variable for the execution file. It is mainly used to regulate the directory for command search. Each directory is ordered and separated by the colon ":" in the middle of each directory, as mentioned in the preceding example! So what is the significance of the PATH variable?

If you move ls to the/root directory (mv/bin/ls/root), and then you are also under/root (cd/root ), but when you execute ls, he just ignores you? What should I do? This is because the PATH variable does not have the/root directory, and you have moved the ls to the/root directory, so the natural system cannot find the executable file, so it will tell you, command not found! So how can we overcome this problem? There are two methods, one: directly add the/root PATH to the PATH! How to add? Available:

[Root @ linux ~] # PATH = "$ PATH":/root

In this way, add the PATH search directory! Another method is to use the full file name to issue instructions, that is, to directly use relative or absolute paths for execution. 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 should be very common in the future!

If I have two ls files in different directories, such as/usr/local/bin/ls and/bin/ls, Which ls will be executed when I release ls? In other words, find out which directory in the PATH is first queried, then the files under that directory will be first executed!

Success! In this case, why not add the. Directory to the PATH. In this case, can the file be executed directly in the directory where it is located? Because. represents the directory! That's right! But have you ever wondered if a strange 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? Yes, it may win the bid, so cotton is not recommended to add "." To the PATH search for safety!

For more information about PATH and related "variables" and "environment variables", we will introduce cotton more deeply in the third article bash shell, you should also be able to understand the meaning of the sentence "why is the absolute path method more correct than the relative path, because the command is directly found for execution, rather than searching through the content of the PATH variable!
Related Article

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.