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..
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$PATHlookups (see Command Search and Execution).For example, suppose your
$PATHis/opt/local/bin:/usr/bin, and you havegrepinstalled in only/usr/bin. When you executegrep, you get the obvious result:Now suppose that you install a newer version of
grepinto/opt/local/bin, which is earlier in your$PATHthan/usr/bin. Becausewhichalways does the full$PATHlookup each time, but bash keeps a hash table, bash still thinks that the commandgrepmaps to the one in/usr/bin:You can use the
typebuiltin to diagnose this problem.typewill 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:So how do you fix this? You can use the
hashbuiltin to manipulate the hash table (typehelp hashfor more information). If you just want to fix one entry (grepin this case), you can dohash -d grepto say “delete the hash table entry forgrep“, in which case the next time you executegrep, it will search the full$PATHas 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 usehash -rto empty it.