typeSee exactly what the shell will run for a name
type tells you how the shell will interpret a command name: an alias, a keyword, a function, a builtin, a cached path or a file found in PATH. When a command behaves strangely, type -a shows every definition in the order the shell tries them.
type [-a] [-t | -p | -P] NAME...Learn what type reports for each kind of name before you need it during an incident.
typeA shell builtin, so it sees the shell's aliases, functions and hash table.
When bash runs a command, it decides what the first word means in a fixed order. Aliases are expanded first, when the line is read (interactive shells only). Then the parser recognises reserved words like if, for and [[. At execution time it checks shell functions, then builtins (cd, echo, export, which must run inside the shell to change its state), then its hash table of remembered paths, and finally walks the PATH directories left to right; the first executable file wins and is exec'd. If nothing matches, bash reports 'command not found' with exit status 127. type is a builtin that reports this decision without running anything: 'is aliased to', 'is a shell keyword', 'is a function' (and prints its body), 'is a shell builtin', 'is hashed (path)' or 'is /path'. -a lists every match, including files in PATH that are shadowed by a builtin or alias. -t prints a one-word category for scripts. -P forces a PATH search even for builtins. zsh's type uses slightly different wording ('is an alias for').
bash tries aliases, keywords, functions, builtins, its hash table, then PATH in order; type reports which step wins.
When the line is read, bash checks the first word against aliases. ll becomes 'ls -alF', then ls is checked again and becomes 'ls --color=auto -alF'. Scripts skip this step.
type NAMEHow NAME will be interpreted-aAll definitions in lookup order-tOne word: alias, keyword, function, builtin, file (or nothing)-pPath of the file that would run (nothing if it's not a file)-PForce a PATH search, even if NAME is a builtin or alias-fIgnore functionscommand NAMERun NAME skipping aliases and functionsUsing which to understand why a command misbehaveswhich only searches PATH and misses the alias, function or stale hash that's actually running.
type -a NAMEParsing 'type' output in scriptsThe wording differs between bash, zsh and dash.
type -t NAME (bash) or command -v NAME (POSIX)Naming a script or function 'test', 'time' or 'cd'Builtins and keywords with those names win, so your script never runs when called by name.
Pick unique names; check with type -a firstTry these on a Linux machine of your own. Each task has a hint and a solution.
The sandbox shell is zsh. Run 'type ll ls cd for' in zsh, then in bash, and compare the wording.
In bash, list every definition of echo and of pwd.
Define a function called ls that prints 'hijacked', confirm with type that it wins, then remove it.
Print only the category (alias, builtin, keyword, file) for ll, cd, if and grep in bash.
typeSee exactly what the shell will run for a name
type tells you how the shell will interpret a command name: an alias, a keyword, a function, a builtin, a cached path or a file found in PATH. When a command behaves strangely, type -a shows every definition in the order the shell tries them.
type [-a] [-t | -p | -P] NAME...Learn what type reports for each kind of name before you need it during an incident.
typeA shell builtin, so it sees the shell's aliases, functions and hash table.
When bash runs a command, it decides what the first word means in a fixed order. Aliases are expanded first, when the line is read (interactive shells only). Then the parser recognises reserved words like if, for and [[. At execution time it checks shell functions, then builtins (cd, echo, export, which must run inside the shell to change its state), then its hash table of remembered paths, and finally walks the PATH directories left to right; the first executable file wins and is exec'd. If nothing matches, bash reports 'command not found' with exit status 127. type is a builtin that reports this decision without running anything: 'is aliased to', 'is a shell keyword', 'is a function' (and prints its body), 'is a shell builtin', 'is hashed (path)' or 'is /path'. -a lists every match, including files in PATH that are shadowed by a builtin or alias. -t prints a one-word category for scripts. -P forces a PATH search even for builtins. zsh's type uses slightly different wording ('is an alias for').
bash tries aliases, keywords, functions, builtins, its hash table, then PATH in order; type reports which step wins.
When the line is read, bash checks the first word against aliases. ll becomes 'ls -alF', then ls is checked again and becomes 'ls --color=auto -alF'. Scripts skip this step.
type NAMEHow NAME will be interpreted-aAll definitions in lookup order-tOne word: alias, keyword, function, builtin, file (or nothing)-pPath of the file that would run (nothing if it's not a file)-PForce a PATH search, even if NAME is a builtin or alias-fIgnore functionscommand NAMERun NAME skipping aliases and functionsUsing which to understand why a command misbehaveswhich only searches PATH and misses the alias, function or stale hash that's actually running.
type -a NAMEParsing 'type' output in scriptsThe wording differs between bash, zsh and dash.
type -t NAME (bash) or command -v NAME (POSIX)Naming a script or function 'test', 'time' or 'cd'Builtins and keywords with those names win, so your script never runs when called by name.
Pick unique names; check with type -a firstTry these on a Linux machine of your own. Each task has a hint and a solution.
The sandbox shell is zsh. Run 'type ll ls cd for' in zsh, then in bash, and compare the wording.
In bash, list every definition of echo and of pwd.
Define a function called ls that prints 'hijacked', confirm with type that it wins, then remove it.
Print only the category (alias, builtin, keyword, file) for ll, cd, if and grep in bash.