How BASH Shell Command Search Sequence Works

This article was organically contributed by monk.

HASH tables and PATH is not the first method locating your program or executable files on a Linux or Unix-like systems. 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:

  1. Before a command is executed REDIRECTION is done. Then following sequence used by SHELL
  3. Parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable
  4. Shell FUNCTION
  5. BUILTIN command
  6. HASH tables
  7. PATH variable
  8. If everything fails, you see command not found error message.


Try out the following simple examples to understand the SHELL sequence

a) Create your own bin directory and add to a PATH:
$ mkdir ~/bin
$ export PATH=$PATH:~/bin

b) Create a shell function called hello:
$ function hello() { echo "Hello from function()" ; }

c) Create an alias called hello:
$ alias hello='echo Hello from alias'

d) Create a shell 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) Let us test it:
Now, you have three different commands with the same name (i.e. hello). Which one will execute first? Type the following commands at command prompt:
$ type hello
$ hello

Sample outputs:

Hello from alias

f) Remove an alias and run hello again:
$ unalias hello
$ hello

Sample outputs:

Hello from function()

g) Remove a shell function hello and run hello again:
$ unset hello
Sample outputs:

Hello for script

h) Remove a shell script hello and run hello again:
$ rm ~/bin/hello
$ hello

Sample outputs:

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? The answer is pretty simple, “HASH table. The shell is looking for a cached PATH entry for hello script. To verify this just type the hash command to see HASH table:
$ hash
Sample outputs:

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 names in HASH table. There is an easy solution, just clear hash table and see the effect again:
$ hash -r
$ hello
Sample outputs:

bash: hello: command not found

I hope this basic explanation will help to all new users 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

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Anoop Jul 15, 2011 @ 18:02

    Thank you! I found the hash -r part very useful!

  • Nizzy Oct 29, 2009 @ 21:55

    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?

  • Nizzy Oct 29, 2009 @ 21:00

    somehow the word function does not execute.

    which is correct?
    function func_name() {
    func_name() {
    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

  • Philippe Apr 24, 2009 @ 13:54

    Very interesting and informative. Thank you for taking the time to post this.

  • Diptanjan Jun 12, 2007 @ 13:24

    Simple and to the point.

  • sathiya Jun 4, 2006 @ 16:17

    simply superb

  • Anonymous Feb 19, 2006 @ 11:52

    The description while simple, is acturate enough for most modern shells (zsh, bash, *ksh, *csh)



    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.

  • Anonymous Feb 13, 2006 @ 1:21

    Built-in command –> external commands.

    I think the author used bash shell as an example here, so it is BASH specific stuff.

    Best Regards,


  • jmarki Feb 13, 2006 @ 1:05

    Is this only for the bash shell or for most of the common shells being used (tcsh, sh)?

  • Anonymous Feb 12, 2006 @ 23:37

    great stuff but why cant u explain other topic also like “PAM” “CRON” ?

  • Hatim Feb 12, 2006 @ 20:29


  • Vurdlak Feb 11, 2006 @ 20:25

    this is great stuff can i get permission to publish it on my c++ maniac blog?? i’ll inlude your link!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @