Command resolution and hashing
In ordinary non-POSIX Bash, after parsing and alias handling, a command name without a slash is resolved as a function, then an enabled builtin, then an executable found through PATH (possibly using the hash cache). A name containing a slash selects a path directly. type explains resolution; type -a lists alternatives. command bypasses function lookup for its operand, while builtin explicitly requests a builtin. enable -n name disables a builtin in the current shell; enable name restores it. Avoid changing builtins in normal scripts unless necessary. hash -t name reports the remembered path; hash -r clears cached paths, not files. Assigning PATH also clears the cache. With checkhash enabled, Bash checks that a cached path still exists before using it. In a clean shell without a command-not-found handler, missing commands return 127; a located nonexecutable file can return 126. Use only fixed trusted names and paths in these examples. The cache remembers executable paths, not command output. command -p uses a default utility search path; it is not the same as builtin, which only invokes builtins. The cache example assumes an available external cat and checks the lookup before printing its result.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Bypass a shadowing function
printf() { builtin printf 'function\n'; }type -t printfprintf ignoredcommand printf 'command\n'builtin printf 'builtin\n'Expected stdout:
functionfunctioncommandbuiltincommand and builtin bypass the function in this fixture.
Cache an executable path
hash cat || exit 1path=$(hash -t cat) || exit 1[[ -x $path ]] && printf 'cached executable\n'hash -r || exit 1if hash -t cat 2>/dev/null; then printf 'unexpected\n'; else printf 'cache cleared\n'; fiExpected stdout:
cached executablecache clearedNo absolute path is assumed; the check tests the discovered path.
Disable and restore a builtin locally
type -t echoenable -n echotype -t echoenable echotype -t echoExpected stdout:
builtinfilebuiltinIn this fixture the external echo is available on PATH. The change is limited to this shell.
Common mistakes
command -v does not prove that a name is an external executable: it may report a function or builtin. Inspect with type. Do not execute user-controlled strings as shell syntax.
Practice
Define a function called printf in a fresh shell. Compare type -t printf, command printf and builtin printf; then inspect and clear the hash entry of cat.
Next: exec and time