
This article was organically contributed by monk.
HASH tables and PATH is not the first method locating your program / executable files. Your program can be a shell function or builtin command or an alias. Here is the complete sequence adopted by BASH shell to execute your command:
- Before a command is executed REDIRECTION is done. Then following sequence used by SHELL
- ALIASES
- Parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable
- Shell FUNCTION
- BUILTIN command
- HASH tables
- PATH variable
- If everything fails, you see command not found error message.
Examples
Try out following simple example to clear your idea about SHELL sequence
a) Create your own bin directory and add to a PATH:
$ mkdir ~/bin
$ export PATH=$PATH:~/bin
b) Create function called hello:
$ function hello() { echo "Hello from function()" ; }
c) Create alias called hello:
$ alias hello='echo Hello from alias'
d) Create script called hello in /home/you/bin directory (which is added to your PATH)
$ echo 'echo Hello from script' > ~/bin/hello
$ chmod +x ~/bin/hello
e) Test it...
Now you have three different commands with same name (i.e. hello). Which one will execute first? Type hello command and find out if above sequences works or not:
$ type hello
$ hello
Output:
Hello from alias
f) Remove alias and run hello again:
$ unalias hello
$ hello
Output:
Hello from function()
g) Remove function hello and run hello again:
$ unset hello
Output:
Hello for script
h) Remove script hello and run hello again:
$ rm ~/bin/hello
$ hello
Output:
bash: /home/monk/bin/hello: No such file or directory
You just removed the hello script but Bash shell still looking hello program at /home/monk/bin/hello location. What is going on here?
Answer is pretty simple - HASH table. (shell is looking for a cached PATH entry for hello script). To verify this just type hash command to see HASH table:
$ hash
Output:
hits command 1 /bin/rm 1 /bin/cat 2 /usr/bin/print 3 /home/monk/bin/hello 1 /usr/bin/man
Your SHELL remembered path settings name in HASH table. There is an easy solution, just clear hash table and see the effect again:
$ hash -r
$ hello
Output:
bash: hello: command not found
I hope this basic explanation will help to all newbie(s) out there. As a result next time you run a command remember shell goes through quite a complicated sequence of operations to process your request.
Continue reading the second part of "How Linux or UNIX Understand which program to run" series (this is part I).
- PART I : How Linux or UNIX Understand which program to run
- PART II : An example: How shell Understand which program to run
Updated for accuracy.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email this to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: Aug/29/2007


{ 11 comments… read them below or add one }
this is great stuff can i get permission to publish it on my c++ maniac blog?? i’ll inlude your link!
Neat
great stuff but why cant u explain other topic also like “PAM” “CRON” ?
PRASHANT K
Is this only for the bash shell or for most of the common shells being used (tcsh, sh)?
Built-in command –> external commands.
I think the author used bash shell as an example here, so it is BASH specific stuff.
Best Regards,
Mike
The description while simple, is acturate enough for most modern shells (zsh, bash, *ksh, *csh)
Regards,
Pete
PS: Aliases, Functions and Builtins have to be above external commands in the search order as the shell can not be customised internally if external commands are given priority. Consider, alias ls=’ls -aF’ as an example, this allow a user to essentially customise there POSIX Environment (Standardised UNIX Like Environment — Linux is a KERNEL, not an operating system) without the need for external commands.
simply superb
Simple and to the point.
Very interesting and informative. Thank you for taking the time to post this.
somehow the word function does not execute.
which is correct?
function func_name() {
}
or
func_name() {
}
or
function func_name {
}
if put the plain test into hello file, then command hello prints. but if I make it a function, it returns nothing, but does not give error.
please help
I finally executed the function, however, it will not execute in the next login even if I have the path in the PATH.
if I source the func_file, then I can execute, but then I have to source each time..why?