[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hash not restored after running command -p

From: Chet Ramey
Subject: Re: hash not restored after running command -p
Date: Mon, 1 Nov 2021 11:26:00 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.2.1

On 10/31/21 12:06 PM, Roger Morris wrote:
> Thanks for the reply.  Though POSIX may allow this, still the last
> line of the following example is rather unexpected behavior
> $
> $ echo echo MY LOCAL tmp/date SCRIPT > tmp/date
> $ chmod +x tmp/date
> $
> $ PATH=.:/bin
> $ date
> Sun 31 Oct 2021 11:59:07 AM EDT
> $ hash -l
> builtin hash -p /bin/date date
> $ cd tmp ; date ; cd ..
> MY LOCAL tmp/date SCRIPT

This seems weird and non-intuitive (why not use the hashed value?), but
it's actually a special case that's handled explicitly in the code. If the
PATH search that results in a value being put into the hash table finds `.'
in the PATH before finding the full path, the hash table remembers that
fact and -- as a special case -- checks whether `./name' is an executable
file before returning the hashed value on subsequent lookups. It's strange,
but everyone seems to do it.

> $
> $ PATH=.:/bin
> $ command -p date
> Sun 31 Oct 2021 11:59:50 AM EDT
> $ hash -l
> builtin hash -p /bin/date date
> $ cd tmp ; date ; cd ..
> Sun 31 Oct 2021 12:00:03 PM EDT

Once you know the above, this makes sense: `command -p' uses a magic value
for PATH, and since that value doesn't include `.' the search doesn't find
`.'. This means the full path gets put into the hash table without the flag
that says `look at ./date first', and the hashed value gets used.

The behavior would, of course, differ if `command -p' didn't do anything
with the hash table.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]