Search code examples
linuxexecution

Order of files to be executed in linux and how to change it


What is the order in which executables are searched by linux for executing when a command is executed. I have following:

  1. /bin/play
  2. /home/prabhat/playframework/play

both the above files are in path.

When I execute it from my home directory as in below

prabhat@localhost~/home/prabhat/$ play

/bin/play gets executed.

What is the order in which linux searches for files to be executed. Is there a way to change it?


Solution

  • The order is defined by the entry order in your PATH variable.

    Note that your shell may cache this info and when you change the PATH you may need to refresh this cache. In bash this is done by the hash command. See here for more information.

    hash command maintains a hash table, which has the used command’s path names. When you execute a command, it searches for a command in the variable $PATH. But if the command is available in the hash table, it picks up from there and executes it