CentOS 7.3 Verifies the priority of Linux command execution

Source: Internet
Author: User

In Linux, different directories may have the same file. Despite the same name, the two files have a completely different effect.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" no title "border=" 0 "alt=" Untitled "src=" http://s3.51cto.com/wyfs02/M01/9B/FB/ Wkiol1lp2kazv5jraafwclo3hxs016.png "width=" 831 "height=" 194 "/>

Executing the same command may show a completely different result. If the PWD originally displays the current working directory, we can define an alias to make it output time.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 1 "border=" 0 "alt=" Untitled 1 "src=" http://s3.51cto.com/wyfs02/M02/9B/FC/ Wkiom1lp2kebhzxpaadsr54jdua487.png "width=" 838 "height=" 223 "/>

To understand the order of command execution, first we need to understand the differences between internal commands and external commands. Use Type command to view the command type.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 2 "border=" 0 "alt=" Untitled 2 "src=" http://s3.51cto.com/wyfs02/M02/9B/FC/ Wkiom1lp2kerajavaade93qq1eg038.png "width=" 838 "height="/>

We can see that the above four commands show a completely different result.

Here the PWD is an internal command, Cal is a hash (cache) command, LS is an alias, and date is an external command.

To introduce a variable path, we can look at the value of the variable path through ECHO.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 3 "border=" 0 "alt=" Untitled 3 "src=" http://s3.51cto.com/wyfs02/M00/9B/FC/ Wkiom1lp2kixiyk3aacds-wy7tu139.png "width=" 850 "height=" "/>

CentOS 6.9 differs from the value of path in CentOS 7.3.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 4 "border=" 0 "alt=" Untitled 4 "src=" http://s3.51cto.com/wyfs02/M00/9B/FB/ Wkiol1lp2kmgus7vaacovwp_pri416.png "width=" 857 "height=" "/>

But the path in both represents the order in which the shell search commands are executed.

In CentOS 7.3 We can use Whereis to see the PWD path.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 5 "border=" 0 "alt=" Untitled 5 "src=" http://s3.51cto.com/wyfs02/M00/9B/FB/ Wkiol1lp2kqqmqynaacxvl5m7gc553.png "width=" 863 "height="/>

You can see that the PWD path at this time is/usr/bin/pwd, and the following two are the help documents for the PWD command.

At this point we execute the enable-n pwd command, disabling the function of the PWD original internal command.

Under this type, the PWD will be found as an external command.

We copied the/usr/sbin/ifconfig to/usr/loacl/sbin and named PWD.

Performing the PWD will output the function of Ifconfig display IP address.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled 9 "border=" 0 "alt=" Untitled 9 "src=" http://s3.51cto.com/wyfs02/M01/9B/FB/ Wkiol1lp2k6hku68aasrknt6ak0542.png "width=" 870 "height=" 509 "/>

As you can see, the order in which the external commands are executed is searched by the value of the path variable.

CentOS 7.3 path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin. The colon is a separator character.

In the above, the PWD under/usr/local/sbin/takes precedence over the PWD under/usr/bin, performing the results of/USR/LOCAL/SBIN/PWD.

At this point we can use RM-RF to delete/usr/local/sbin/pwd. Only/usr/bin/pwd exist at this time.

To perform the PWD, we found that the current working directory was not output. Instead it shows that/USR/LOCAL/SBIN/PWD does not exist.

Type pwd, at which time the PWD is the hash command.

hash-d pwd, remove the PWD cache. The re-execution Pwd,pwd finally shows the current working directory.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled Ten "border=" 0 "alt=" Untitled "src=" http://s3.51cto.com/wyfs02/M01/9B/FB/ Wkiol1lp2k-we9zqaaj_1esi2_4617.png "width=" 901 "height="/>

The above proves that the execution of the hash command takes precedence over path.

At this time we type PWD.

Clear the PWD hash entry again. Copy the ifconfig to/usr/local/sbin/pwd again to make the PWD output ifconfig effect.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled One "border=" 0 "alt=" Untitled "src=" http://s3.51cto.com/wyfs02/M02/9B/FB/ Wkiol1lp2lhscvzyaalwy2xvpq4201.png "width=" 902 "height=" 482 "/>

Type below, pwd again becomes the hash command.

Enable PWD to revert the PWD to internal commands.

Perform PWD, output as current working directory.

The internal command execution takes precedence over the hash.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" Untitled "border=" 0 "alt=" Untitled "src=" http://s3.51cto.com/wyfs02/M00/9B/FC/ Wkiom1lp2lkgjh-9aai7vppy47i110.png "width=" 899 "height=" 481 "/>

Alias Pwd=cal, set the nickname for Pwd to Cal.

To perform the PWD, we will find the PWD output the calendar of the month.

It is known that alias execution takes precedence over builtin (internal command).

In summary, the command execution priority is: alias > Internal command > Hash > PATH. Of course, if you can't find it in path, you can only command not found.

650) this.width=650; "Style=" background-image:none; border-right-0px; padding-left:0px; padding-right:0px; border-top-width:0px; border-bottom-width:0px; border-left-width:0px; padding-top:0px "title=" no title "border=" 0 "alt=" Untitled "src=" http://s3.51cto.com/wyfs02/M01/9B/FC/ Wkiom1lp2lob4tyiaacbq3x7v5a094.png "width=" 905 "height="/>

CentOS 7.3 Verifies the priority of Linux command execution

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.