I'm assuming it has something to do with when the program to run is created, it is associated with the working directory where it was created ?
Assuming you are calling the program without a full or relative path and the program is not a function or built-in, it must be in your PATH environmental variable in order to be found.
Bash uses a hash table to remember the full pathnames of executable files to avoid multiple PATH searches (see the description of hash in Bourne Shell Builtins). A full search of the directories in $PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked in a separate execution environment with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of that subshell. If that function is not defined, the shell prints an error message and returns an exit status of 127.
When a program is executed the invoking program (typically a shell), by convention, searches for the program in the directories listed in the
PATH environment variable. To do this, the invoking program can use library functions like
execvpe, which do the search, or the invoking program can implement similar logic itself.
The library functions eventually call the
execve system call, which is the workhorse that actually executes the program. The
execve call takes a file name as first parameter, which is the name of the file containing the program. The name is usually an absolute path to the file, but is can also be a relative path. In this case the search for the file is done starting from the process's current directory. (Note that the file is not necessarily contained in the current directory: the name can be, for instance,
execve system call does not care about the
PATH environment variable.
So, how does the kernel know the absolute path to the program if a relative path is given to the
execve system call? The answer is that it doesn't, and it doesn't need to.