PATH and command lookup
What you need to know
PATH is a colon-separated list of directories Bash searches for an executable when a command name has no slash and normal command resolution reaches external lookup. Builtins and functions can take precedence; Bash may cache executable locations. type tells how a name resolves; command -v reports the command resolution. Use a fresh lab shell; never replace your real PATH just to test a lesson.
Run each example separately as a new Bash script (Chapter 1). Tested with Bash 5.2.37 on Debian 13, default shell options; no personal configuration is required.
Recognize a builtin
Expected stdout:
builtinprintfIn a clean Bash shell printf resolves to a builtin. type -t reports the kind rather than printing a full description.
Search a controlled PATH
Observed output on Debian 13:
/usr/bin/bashThis result was observed on Debian 13; other systems can return a different path or no result. The child-only assignment avoids modifying the parent PATH. A path containing a slash bypasses PATH search.
Inspect location variables
printf 'home=%s\n' "$HOME"cd / && printf 'pwd=%s\n' "$PWD"Illustrative output (values vary):
home=<your home directory>pwd=/HOME commonly identifies a home directory; PWD records the current working directory maintained by Bash; after a successful cd /, PWD is /. The && guard prints the new path only on success. OLDPWD records the previous directory after successful cd. The first output depends on your environment; the lab uses a synthetic HOME. Run this in a child script so your terminal directory is unchanged.
Common mistakes
Do not prepend untrusted or writable-by-others directories to PATH, or introduce empty components (which can mean the current directory). command not found can mean a missing program or a lookup problem; use type and command -v before changing settings.
Practice
In a fresh Bash shell use type -t printf, command -v bash, and command -v bm_missing_command. Compare the exit statuses immediately after each.
Next lesson: Special parameters and shell identity