Bug 1410087

Summary: bash searches $PATH only if the command is not already hashed
Product: Red Hat Enterprise Linux 7 Reporter: Marek Haicman <mhaicman>
Component: bashAssignee: Siteshwar Vashisht <svashisht>
Status: CLOSED NOTABUG QA Contact: BaseOS QE - Apps <qe-baseos-apps>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.3CC: kdudka
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-01-05 09:31:59 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Marek Haicman 2017-01-04 12:35:24 UTC
Description of problem:
When binary is available in more than one directory of $PATH, bash can get lost and stops running based on PATH variable, but still uses the same one.

Version-Release number of selected component (if applicable):
bash-4.2.46-21.el7_3.x86_64

How reproducible:
reliably

Steps to Reproduce:
create files a_1.sh and a_2.sh, which echoes 1 and 2 respectively:

Actual results:
[dahaic@dhcp-24-168 ~]$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/dahaic/.local/bin:/home/dahaic/bin
[dahaic@dhcp-24-168 ~]$ sudo cp a_1.sh /usr/bin/a.sh
[dahaic@dhcp-24-168 ~]$ a.sh
1
[dahaic@dhcp-24-168 ~]$ sudo cp a_2.sh /usr/local/bin/a.sh
[dahaic@dhcp-24-168 ~]$ a.sh
2
[dahaic@dhcp-24-168 ~]$ sudo rm /usr/local/bin/a.sh 
[dahaic@dhcp-24-168 ~]$ a.sh
1
[dahaic@dhcp-24-168 ~]$ sudo cp a_2.sh /usr/local/bin/a.sh

!!! it breaks !!!
[dahaic@dhcp-24-168 ~]$ a.sh
1
[dahaic@dhcp-24-168 ~]$ which a.sh
/usr/local/bin/a.sh
[dahaic@dhcp-24-168 ~]$ /usr/local/bin/a.sh 
2
[dahaic@dhcp-24-168 ~]$ a.sh
1

Expected results:
Binary reported by which is run by bash

Additional info:

Comment 1 Kamil Dudka 2017-01-04 12:58:55 UTC
(In reply to Marek Haicman from comment #0)
> Actual results:
> [dahaic@dhcp-24-168 ~]$ echo $PATH
> /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/dahaic/.
> local/bin:/home/dahaic/bin
> [dahaic@dhcp-24-168 ~]$ sudo cp a_1.sh /usr/bin/a.sh
> [dahaic@dhcp-24-168 ~]$ a.sh
> 1
> [dahaic@dhcp-24-168 ~]$ sudo cp a_2.sh /usr/local/bin/a.sh
> [dahaic@dhcp-24-168 ~]$ a.sh
> 2

For me it already prints 1 in the above step, which is the documented behavior of bash -- see the bash(1) man page:

    Bash uses a hash table to remember the full pathnames of executable files
    (see hash under SHELL BUILTIN COMMANDS below).  A full search of the
    directories in PATH is performed only if the command is not found in the
    hash table.

So just use the 'hash' built-in to update the hash table in cases like this.

Comment 2 Marek Haicman 2017-01-04 14:30:03 UTC
Thank you for the explanation, Kamil.

Can we at least ignore the hash table, when binary is not found in the location? It happened to me few times :)

[dahaic@dhcp-24-168 openscap]$ oscap --version
bash: /usr/bin/oscap: No such file or directory
[dahaic@dhcp-24-168 openscap]$ hash -r
[dahaic@dhcp-24-168 openscap]$ oscap --version
OpenSCAP command line tool (oscap) 1.2.13

Comment 3 Kamil Dudka 2017-01-04 15:28:31 UTC
(In reply to Marek Haicman from comment #2)
> Can we at least ignore the hash table, when binary is not found in the
> location?

It would contradict the current documentation for no real benefit.  Even if you convince upstream that it is a good idea, the behavior is not going to change in any released products.

> It happened to me few times :)

Given the fact that the hashing affects only the current session, it should not happen in usual workflows.  Why do you remove /usr/bin/oscap?

If you do it intentionally to force using oscap from a different location, then just invoke 'hash oscap' to make it explicit.

> [dahaic@dhcp-24-168 openscap]$ oscap --version
> bash: /usr/bin/oscap: No such file or directory
> [dahaic@dhcp-24-168 openscap]$ hash -r
> [dahaic@dhcp-24-168 openscap]$ oscap --version
> OpenSCAP command line tool (oscap) 1.2.13

Note that the hashing can also be disabled via 'set +h'.

Comment 4 Marek Haicman 2017-01-04 18:58:09 UTC
For posterity - I was doing make uninstall, to check whether version provided via RPM has the same error as the upstream version.

Even though it's not very transparent behaviour for someone not aware of the hashing, it does behave in documented way.

I have no more objections and we can close it as NOTABUG.

Comment 5 Kamil Dudka 2017-01-05 09:31:59 UTC
Thank you for clarifying it!  Closing...