CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / type

type

See exactly what the shell will run for a name

Intermediateshellpathtroubleshooting

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.

Syntaxtype [-a] [-t | -p | -P] NAME...

Watch it run

Learn what type reports for each kind of name before you need it during an incident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
type ll cd ls for

An alias shows its replacement text. cd must be a builtin: a separate program couldn't change your shell's directory. for is part of the shell's grammar.

Anatomy of a command

$
commandtype

A shell builtin, so it sees the shell's aliases, functions and hash table.

How it works

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.

llmissfirst matchno matchYou typell /var/log1. Aliastext replacell -> ls -alF2. Keywordif for [[3. Functiondefined in shell4. Builtincd echo export5. Hash tablecached paths6. PATH searchleft to rightexecve/usr/bin/lsNot foundexit 127
1/8
Aliases are expanded first

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.

← → to step

Options you'll actually use

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 functions
command NAMERun NAME skipping aliases and functions

Common mistakes

Using which to understand why a command misbehaves

which only searches PATH and misses the alias, function or stale hash that's actually running.

type -a NAME
Parsing 'type' output in scripts

The 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 first

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    The sandbox shell is zsh. Run 'type ll ls cd for' in zsh, then in bash, and compare the wording.

  2. 2

    In bash, list every definition of echo and of pwd.

  3. 3

    Define a function called ls that prints 'hijacked', confirm with type that it wins, then remove it.

  4. 4

    Print only the category (alias, builtin, keyword, file) for ll, cd, if and grep in bash.

Related commands

whichFind which executable runs for a command namealiasCreate shortcuts for commands you type all the timeexportPass variables to the programs you startbash: functionsFunctions: name a block, pass arguments, return a status
PreviouswhichNextgrep
Linux / Environment & Shell / type

type

See exactly what the shell will run for a name

Intermediateshellpathtroubleshooting

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.

Syntaxtype [-a] [-t | -p | -P] NAME...

Watch it run

Learn what type reports for each kind of name before you need it during an incident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
type ll cd ls for

An alias shows its replacement text. cd must be a builtin: a separate program couldn't change your shell's directory. for is part of the shell's grammar.

Anatomy of a command

$
commandtype

A shell builtin, so it sees the shell's aliases, functions and hash table.

How it works

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.

llmissfirst matchno matchYou typell /var/log1. Aliastext replacell -> ls -alF2. Keywordif for [[3. Functiondefined in shell4. Builtincd echo export5. Hash tablecached paths6. PATH searchleft to rightexecve/usr/bin/lsNot foundexit 127
1/8
Aliases are expanded first

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.

← → to step

Options you'll actually use

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 functions
command NAMERun NAME skipping aliases and functions

Common mistakes

Using which to understand why a command misbehaves

which only searches PATH and misses the alias, function or stale hash that's actually running.

type -a NAME
Parsing 'type' output in scripts

The 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 first

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    The sandbox shell is zsh. Run 'type ll ls cd for' in zsh, then in bash, and compare the wording.

  2. 2

    In bash, list every definition of echo and of pwd.

  3. 3

    Define a function called ls that prints 'hijacked', confirm with type that it wins, then remove it.

  4. 4

    Print only the category (alias, builtin, keyword, file) for ll, cd, if and grep in bash.

Related commands

whichFind which executable runs for a command namealiasCreate shortcuts for commands you type all the timeexportPass variables to the programs you startbash: functionsFunctions: name a block, pass arguments, return a status
PreviouswhichNextgrep