bashshelltypesmkdirscite

Bash 'type someCmd' : what means 'hashed'? (different outputs for 'type mkdir')


What puzzles me is, that I am getting different output running this two-liner script:

help | head -n1
type mkdir

from within SciTE (here the SciTE output panel output):

GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)
mkdir is /bin/mkdir

and from within a Terminal window (here the Terminal output):

GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)
mkdir is hashed (/bin/mkdir)

How does it come that the same script executed by the same shell gives not the same output?


Solution

  • The difference is situational:

    The first time bash executes an external utility in a given session by filename only (whether successfully or not), it remembers its full path for faster re-execution later (saving the need to look for the utility in all directories listed in the $PATH variable again).

    This remembering (caching) is called hashing and happens implicitly via builtin hash.

    Therefore, before mkdir is ever executed in a given session, type mkdir returns:

    mkdir is /bin/mkdir
    

    After having executed mkdir at least once, type mkdir then reports:

    mkdir is hashed (/bin/mkdir)
    

    You can tell bash to "forget" all remembered paths with hash -r, or selectively with hash -d <name>; just hash prints all currently hashed paths and their hit counts - see help hash.