Johnyy
Johnyy

Reputation: 2136

Problem: Unable to execute the correct executable file in Unix

I am encountering a really strange problem, any help is appreciated.

I have a executable file compiled and cp to a specific location. The name of this executable is "qact" There is a newly added cout statement at the first line of the main function. But when I execute the binary file in that directory, i can not see it. After a long while I accidentally find out that if I am not in that directory, I can see the outputed string when i execute it.

And later I find out that it's only when I am in that directory, the executed binary file is wrong and I will not see the string.

when I use which on that executable, no mater what directory I am in, I always get the same result and it is the correct location.

Really confused..

Upvotes: 0

Views: 825

Answers (1)

Adam Rosenfield
Adam Rosenfield

Reputation: 400632

Do you have another executable file of the same name elsewhere in your $PATH? If so, it's possible that bash is executing the wrong executable because it uses a hash table to avoid extra $PATH lookups (see Command Search and Execution).

For example, suppose your $PATH is /opt/local/bin:/usr/bin, and you have grep installed in only /usr/bin. When you execute grep, you get the obvious result:

$ echo $PATH
/opt/local/bin:/usr/bin
$ which grep
/usr/bin/grep
$ grep --version
grep (GNU grep) 2.5.1

Now suppose that you install a newer version of grep into /opt/local/bin, which is earlier in your $PATH than /usr/bin. Because which always does the full $PATH lookup each time, but bash keeps a hash table, bash still thinks that the command grep maps to the one in /usr/bin:

$ which grep
/opt/local/bin/grep
$ grep --version
grep (GNU grep) 2.5.1
$ /opt/local/bin/grep --version
GNU grep 2.6.3

You can use the type builtin to diagnose this problem. type will tell you if a command is a shell builtin, an alias, a function, a keyword, or an executable file. If the latter, it tells you the full path to the executable:

$ type grep
grep is hashed (/usr/bin/grep)

So how do you fix this? You can use the hash builtin to manipulate the hash table (type help hash for more information). If you just want to fix one entry (grep in this case), you can do hash -d grep to say "delete the hash table entry for grep", in which case the next time you execute grep, it will search the full $PATH as expected. If you want to clear out the entire hash table (say, if you just installed a large amount of new software or you changed your $PATH), then use hash -r to empty it.

Upvotes: 2

Related Questions