Interview Prep/Shell Scripting/Shells, scripts and execution

What happens when you type a command name? How does Bash decide what to run?

AdvancedConceptShells, scripts and execution
Answer

Lookup order: aliases (interactive only by default), shell keywords (if, [[), functions, builtins (cd, echo, read), then a search of PATH directories left to right, cached in a hash table. For an external program Bash forks, sets up redirections in the child, and calls execve().

type -a echo        # echo is a shell builtin / echo is /usr/bin/echo
command -v kubectl  # path or nothing, exit 1 if missing
hash -r             # forget cached paths after moving a binary

command -v is the portable way to check whether a tool exists.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Shells, scripts and execution